Package: logginginitializer
Initialize Python logging with RWS Datalab defaults.
A tiny module to make it easy to define your logger settings!How to log in Python applicationsIf you already know how to log and just want to use the LoggingInitializer, see :ref:using-LI.At the start of each module, import the Logging library and generate a logger:import logging
logger = logging.getLogger(__name__)This generates an object which knows from which module you’re logging. It will reference the module in the log.In your code, tell the logger to generate log messages where needed:logger.debug("Going to do something important now.")
try:
  something_important()
except:
  logger.error("Something important didn't work!!!")
logger.info("Something important is done.")The methodsdebug(),info()anderror()(we also havewarning()andcritical()) generate log messages at differentlog levels.
Log levels are used to communicate the importance of a log line, to keep logs relevant, and as small as possible (but not smaller).You will probably choose different log levels on different environments: on a production server, most teams will only log
“WARNING”, “ERROR” and “CRITICAL”; on a testing server usually also “INFO” so it’s easier to follow what’s going on,
and when debugging… “DEBUG”. When writing log messages, choose your log levels with this in mind.The default setting for LoggingInitializer is INFO, which means that statements logged with logger.debug() willnotbe logged by default.For more information on how to use the Python logger and info on the different log levels, seethis helpful tutorial in the Python docs.Using LoggingInitializerInclude the LoggingInitializer in your application to quickly set parameters for the Python logger.At the start of your program, populate a LoggingConfig object with the required parameters.parameters for the LoggingConfig objectNameDescriptionRequired?Default valueidentifierIdentifying the program being logged, will be used in file name of the logYesdirectoryDirectory for the log fileNoNonelog_levelDefault log level for log lines without a specified levelNo“INFO”log_formatFormat for log messagesNo“%(levelname)s:%(asctime)s - %(name)s - %(message)s”date_formatFormat for log timestampsNo“%Y/%m/%d %H:%M:%S %p”backup_countNumber of log files to be kept before a file is re-usedNo50Below an example of a logger that writes to StdOut and to a file is portrayed.
The user executing the program should have write rights on the specified directory!config = {
          "identifier": "my_lovely_program",
          "directory": "/var/logs",
      }

logging_config = LoggingConfig(**config)Then create the LoggingInitializer object:logging_initializer = LoggingInitializer(logging_config)If you want to suppress the logging to StdOut (only log to a file), use Quiet Mode:logging_initializer = LoggingInitializer(logging_config, quiet=True)If you want to suppress the logging to a file (only log to StdOut), simply
don’t specify a directory in theLoggingConfig.logging_config = LoggingConfig(identifier="your-package-name")
logging_initializer = LoggingInitializer(logging_config)For more info on setting the log format and date format, seethe Python docs.
If another application is ingesting the logs, do not change the format unannounced.InstallationTo install logging-initializer, do:git clone https://gitlab.com/rwsdatalab/projects/logging-initializer.git
cd logging-initializer
pip install .Run tests (including coverage) with:pip install -r requirements-dev.txt
python setup.py testDocumentationFind the full documentationhere.LicenseCopyright (c) 2022-2023, Rijkswaterstaat