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.basicConfigfunction. - 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
FileHandlerbe 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,nameandmessageseparated 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.Templaterespectively. 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, aValueErroris 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, aValueErroris 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
FileHandleris 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
FileHandleris created, and thus used when opening the output file. If not specified, the value ‘backslashreplace’ is used. Note that ifNoneis 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.configmodule'sdictConfig(). Note that this argument is incompatible with filename or config - if both are present, aValueErroris 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.
Metadata
Release files for iilog-pckg 0.0.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| iilog-pckg-0.0.4.tar.gz | 10.5 kB | Without provenance | Details for iilog-pckg-0.0.4.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| iilog_pckg-0.0.4-py3-none-any.whl | Python 3 | none | any | Details for iilog_pckg-0.0.4-py3-none-any.whl |
Total release size: 20.2 kB
Release files / iilog-pckg-0.0.4.tar.gz
| Download URL | iilog-pckg-0.0.4.tar.gz |
|---|---|
| Size | 10.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
2f03d00e9b44b16726278ed121a7caea9712eb08406d6d3e716960b5f95849e3
|
|
BLAKE2b-256 checksum How to use checksums |
7041066278593cd6c6b3487bbf3400975909687329ff629979cfbad0a5bfc5e8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.10.10
|
Release files / iilog_pckg-0.0.4-py3-none-any.whl
| Download URL | iilog_pckg-0.0.4-py3-none-any.whl |
|---|---|
| Size | 9.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
b0b2f1a44b8388ffe957b7051918933f4f38549e2eff8f840cab8d590fd5c459
|
|
BLAKE2b-256 checksum How to use checksums |
0b697251cf346b233addafc958441467dbc7aa69b27c1b5019dd55b926740a40
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.10.10
|
Security for iilog-pckg 0.0.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.