sakurashinken
sakurashinken

Reputation: 4080

Best location for log.conf in python package

I am using the native logging utility while writing a python package. What is the best location for logging.conf?

I have the following package structure

/package
 setup.py
 .gitingore
 ....
 /dist
     __init__.py
     dosomething.py
     /utils
         /submodule1
             submodule1.py
         /logger
             log.py
 /tests
    ...

do I want to put the logging.conf in with the logger module or in the package root? Is there a better place?

Upvotes: 0

Views: 152

Answers (1)

zezollo
zezollo

Reputation: 5017

Not easy to answer. I post this because it's way too long for a comment.

The answer may depend on the rest of your package's organization (like, have you only this conf file, or some others? May other conf file show up some day later?).

Here is how I organize this. Though I can't say it's the best organization (I don't know if others have a better way than mine after all), it fits my needs pretty well, and I think it might be helpful to share it. I must answer beyond your question "where to put it?" because I use several files for the same settings (but used in different purposes, see below).

So far, I found it good to gather all different conf files in the same directory (this allows to setup one variable containing the path to all settings, rather than a bunch of variables, one for the logging settings, one for some package's specific settings etc.).

The same can apply to only one conf file: if later another conf file is needed, I know where I can put it and I can use the variable that already contains the right path.

In your example, I would put them in package/dist/settings/default/ and leave there the complete default configuration, tracked by git (or your VCS), e.g. a default working "state", that the package can be in when used in production. This file is not meant to be changed by the user. Then in package/dist/settings/dev/ I put the "dev" versions (untracked by git), where I can change the configuration to fit my needs while developing, without being bothered by git anytime (and while keeping the default settings unchanged in the default/ directory). This is especially useful for debugging settings because they change often. (Then, though they're not interesting for logging-only conf file, I mention that the system-wide conf files can be stored in /etc/mypackage/, and the user's redefined settings can be stored in ~/.config/my_package/).

When loading the settings, my package first loads the default configuration, to ensure all values are defined. Then it checks successively if there's any conf file in /etc/mypackage/, then in ~/.config/my_package/ and finally in package/dist/settings/dev/. Each time it finds one, it overrides the (possibly) redefined values. (As my configuration files are all written in yaml, each existing file found is loaded as a dictionary and I just update the default configuration with these new values. This way, I can only change the settings I like in my dev configuration; not mentionning the others will leave them set to their default values).

A good way to learn more is to browse existing projects and see how others do.

Upvotes: 1

Related Questions