Skip to main content

iilog: alternative library to logging

Project description

iilog_logo

Python PyPI LICENSE

iilog is an alternative library to logging

iilog provides a little more comfortable logging experience. It is my (@kyasuda516) own reconfiguration of Python's stdlib logging.


Features

  • A solution for those who feel slightly dissatisfied with the usability of the built-in logging library.
  • Logger instances can be created in a way that is very similar to using the logging.basicConfig function.
  • The usage of accessing the root logger is designed to be as invisible as possible.

Installation

You can install iilog with

python -m pip install iilog-pckg

Otherwise, you can install directly from the github repo using

python -m pip install git+https://github.com/kyasuda516/iilog-pckg.git

Example Usage

Simple example

sample.py:

from iilog.loggers import Logger
import iilog

# define loggers
logger1 = Logger(
    filename='myapp.log', filemode='w', encoding='UTF-8')

import sys
logger2 = Logger(
    format=r'%(asctime)s %(levelname)-8s %(name)-15s %(message)s', 
    datefmt=r'%Y-%m-%d %H:%M:%S',
    stream=sys.stdout, level=iilog.env.WARNING)

formatter = iilog.formatters.Formatter(iilog.formatters.BASIC_FORMAT)
handler = iilog.handlers.RotatingFileHandler(
    filename='dev.log', maxBytes=256, backupCount=3)
handler.setFormatter(formatter)
logger3 = Logger(handlers=[handler])

# logging
logger1.warning('Watch out!')
logger1.info('I told you so')

logger2.warning('Watch out!')
logger2.info('I told you so')

logger3.error('An unknown error occured.')
logger3.error('An unknown error occured.')
logger3.error('An unknown error occured.')
logger3.error('An unknown error occured.')
logger3.error('An unknown error occured.')

The results are as follows:

$ python sample.py
2023-07-01 02:03:04 WARNING  iilog.loggers.Logger.i1 Watch out!

$ cat myapp.log
Watch out!
I told you so

$ cat dev.log
ERROR:iilog.loggers.Logger.i2:An unknown error occured.

$ cat dev.log.1
ERROR:iilog.loggers.Logger.i2:An unknown error occured.
ERROR:iilog.loggers.Logger.i2:An unknown error occured.
ERROR:iilog.loggers.Logger.i2:An unknown error occured.
ERROR:iilog.loggers.Logger.i2:An unknown error occured.
For more details

The iilog.loggers.Logger constructor works in much the same way as the logging library's basicConfig function. The following is the description of logging.basicConfig()'s parameters taken from the official documentation and added with strikethrough decoration according to the unique specification:

  • filename – Specifies that a FileHandler be created, using the specified filename, rather than a StreamHandler.
  • filemode – If filename is specified, open the file in this mode. Defaults to 'a'.
  • format – Use the specified format string for the handler. Defaults to attributes levelname, name and message separated by colons.
  • datefmt – Use the specified date/time format, as accepted by time.strftime().
  • style – If format is specified, use this style for the format string. One of '%', '{' or '$' for printf-style, str.format() or string.Template respectively. Defaults to '%'.
  • level – Set the root logger level to the specified level.
  • stream – Use the specified stream to initialize the StreamHandler. Note that this argument is incompatible with filename - if both are present, a ValueError is raised.
  • handlers – If specified, this should be an iterable of already created handlers to add to the root logger. Any handlers which don’t already have a formatter set will be assigned the default formatter created in this function. Note that this argument is incompatible with filename or stream - if both are present, a ValueError is raised.
  • force – If this keyword argument is specified as true, any existing handlers attached to the root logger are removed and closed, before carrying out the configuration as specified by the other arguments.
  • encoding – If this keyword argument is specified along with filename, its value is used when the FileHandler is created, and thus used when opening the output file.
  • errors – If this keyword argument is specified along with filename, its value is used when the FileHandler is created, and thus used when opening the output file. If not specified, the value ‘backslashreplace’ is used. Note that if None is specified, it will be passed as such to open(), which means that it will be treated the same as passing ‘errors’.

Also, the iilog.formatters.Formatter, iilog.filters.Filter, iilog.handlers.FileHandler, iilog.handlers.StreamHandler classes and so on, which you should use when specifying handlers, are exactly the same as defined in the logging library.


Example of loading a config file

config.yml:

version: 1
formatters:
  simple:
    format: '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
handlers:
  console:
    class: logging.StreamHandler
    level: DEBUG
    formatter: simple
    stream: ext://sys.stdout
loggers:
  foobarExample:
    level: DEBUG
    handlers: 
      - console
    propagate: no

sample2.py:

import iilog
iilog.env.set_config(filename='config.yml', encoding='UTF-8')
logger = iilog.env.get_logger('foobarExample')
logger.info('I told you so')

The results are as follows:

$ python test.py
2023-07-01 02:03:04,005 - foobarExample - INFO - I told you so
For more details

Takes the logging configuration from a file or dictionary. The parameters are as follows:

  • filename – Use the specified filename to read the logging configuration from the file.
  • fileformat – If filename is specified, read it as a file written in this format. Accepts either 'yaml' or 'json'. If not specified, it will be determined based on filename's extension.
  • encoding – If this keyword argument is specified along with filename, its value is used when opening the file.
  • config – Takes the logging configuration from the specified dictionary, as accepted by the logging.config module's dictConfig(). Note that this argument is incompatible with filename or config - if both are present, a ValueError is raised.

For any grammar or examples not listed here, please refer to the documentation (sorry, currently under construction).


License

This software is released under the MIT License, see LICENSE.

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

iilog-pckg-0.0.4.tar.gz (10.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

iilog_pckg-0.0.4-py3-none-any.whl (9.6 kB view details)

Uploaded Python 3

File details

Details for the file iilog-pckg-0.0.4.tar.gz.

File metadata

  • Download URL: iilog-pckg-0.0.4.tar.gz
  • Upload date:
  • Size: 10.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.10

File hashes

Hashes for iilog-pckg-0.0.4.tar.gz
Algorithm Hash digest
SHA256 2f03d00e9b44b16726278ed121a7caea9712eb08406d6d3e716960b5f95849e3
MD5 7f8edda374168038606307e732280897
BLAKE2b-256 7041066278593cd6c6b3487bbf3400975909687329ff629979cfbad0a5bfc5e8

See more details on using hashes here.

File details

Details for the file iilog_pckg-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: iilog_pckg-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 9.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.10

File hashes

Hashes for iilog_pckg-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 b0b2f1a44b8388ffe957b7051918933f4f38549e2eff8f840cab8d590fd5c459
MD5 226d5458e13fc2e0e7c5f229ab8fbb8d
BLAKE2b-256 0b697251cf346b233addafc958441467dbc7aa69b27c1b5019dd55b926740a40

See more details on using hashes here.

Supported by

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