Skip to main content

Fast building of logger from common templates

Project description

Easily build a logger with various usual templates : console, rotating and file.

Installation

pip install archivist-logger

Usage

Build and use the logger

There’s really only object that is to be used, that configures and returns a logger with one of the available configuration templates.

from archivist import LoggerBuilder

logger = LoggerBuilder().build()

message = "Hello"
logger.info("An info saying %s", message)

The default behaviour is to log only to console. To change this behaviour and log to files ontop of logging to console, use one of the built-in templates by specifying the template argument : LoggerBuilder().build(template="rotating"). See the details section for more information about the available templates.

Note that you should instanciate a logger as above only once for the whole program, usually in the main. Logger instances in modules are to be accessed with logger = logging.getLogger(__name__), so that the program from which the logger was called always appears in the log statement.

Details

Setting log level

The main feature of this package, compared to the usual logging.getLogger(__name__), is to choose the level of logging from an environment variable rather than hard-setting it in the code.

The level (for both console and file handlers) is read from variable LOGGER_LEVEL, and defaulted to CRITICAL if it is not found. The level is applied both to the console handler and the optional file handler.

Logging templates

There are three logging behaviours available that must be chosen when building a logger : console, rotating or scheduled.

Template console

When building the logger with template="console", the returned logger will only log statements to the console. When using this template on aws lambda functions, these statements are automatically redirected to cloudwatch.

Template rotating

When building the logger with template="rotating", log files will be written using logging.handlers.RotatingFileHandler, which means that each log file has a size limit, and when the size limit is reached, another log file is created. See the documentation here for details.

The size limit for a single file is set to 1MB, and the maximum number of files is 20. This mode is appropriate for programs that handles a lot of requests

Template scheduled

When building the logger with template="scheduled", a single log file is written per program execution. The launch date and time is appended to the log file name, so that each execution can be uniquely retrieved by its log file. The format is activity-20200101-00h00m00s.log.

This mode is appropriate for periodically triggered programs, where one wants to be able to identify then investigate a specific execution.

Logfiles name and location

By default, when log files are written, they are at the module’s location, with a default filename activity.log (with variations according to the template used). It is generally a bad idea to keep log files there since they won’t be easily accessible in a python environment. Defining custom location and filename is made with (.log extension is automatically appended to the given filename) :

logger = LoggerBuilder().build(template="scheduled", folder="/home/here", filename="tracking")

Note that no log file is written when using the default behaviour which is template console, so those arguments, if given, will be ignored.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

archivist-logger-0.1.1.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

archivist_logger-0.1.1-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

Details for the file archivist-logger-0.1.1.tar.gz.

File metadata

  • Download URL: archivist-logger-0.1.1.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for archivist-logger-0.1.1.tar.gz
Algorithm Hash digest
SHA256 6aeabb2c20bfb0123243c6612ba38e632795f572fb71a9b5b9c5b06c490a7677
MD5 32ce1c97a5d89c83ac5b08a9999a6bb6
BLAKE2b-256 c325adec49525a00e0b91be43223b6cdb5d16bd823fd0bdd4c65fc390813ae45

See more details on using hashes here.

File details

Details for the file archivist_logger-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for archivist_logger-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3c510919f441df6d913b513c17f66724127020b2c897415a35e8ee6599ece7ae
MD5 e7d381c0143764c78370c8eb71b2933a
BLAKE2b-256 fe9f8bcfb364f220b239442abb9caf8ff0c0fb6158883bbe2382b3354b9b0e8d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page