iilog: alternative library to logging
Project description
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 aStreamHandler
. - 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
andmessage
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()
orstring.Template
respectively. Defaults to'%'
. - level – Set the
rootlogger 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, aValueError
is raised. - handlers – If specified, this should be an iterable of already created handlers to add to the
rootlogger. 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, aValueError
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 ifNone
is specified, it will be passed as such toopen()
, 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'sdictConfig()
. Note that this argument is incompatible with filename or config - if both are present, aValueError
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for iilog_pckg-0.0.4-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b0b2f1a44b8388ffe957b7051918933f4f38549e2eff8f840cab8d590fd5c459 |
|
MD5 | 226d5458e13fc2e0e7c5f229ab8fbb8d |
|
BLAKE2b-256 | 0b697251cf346b233addafc958441467dbc7aa69b27c1b5019dd55b926740a40 |