Simple logger in python mainly used for CLI tool like argparse
Project description
pyplelogger
Simple logger written in python mostly for command line interface tools.
Install
pip install pyplelogger
Usage
Simple usage
This is the atomic usage. Import this and print it out.
from pyplelogger import Logger
log = Logger(__name__).build()
log.info("hogehoge")
INFO 2018-11-26 23:11:15,109 test.py:main in line 4: hogehoge
The default log level is INFO
and you have to pass unique name for each handler.
Change default logger level
You can change logger level entire the project.
import logging
from pyplelogger import Logger
Logger.set_default_log_level(logging.WARNING)
log = Logger(__name__).build()
log.info("hogehoge")
#=> Nothing is pritted out
And once you set teh default log level, it is valid in entire project.
The logger levels are defined in logging
library.
level | number |
---|---|
CRITICAL | 50 |
ERROR | 40 |
WARNING | 30 |
INFO | 20 |
DEBUG | 10 |
NOTSET | 0 |
Let's say you have a script1.py
and script2.py
.
If you change defaul log level in script1.py
like this,
import logging
from pyplelogger import Logger
Logger.set_default_log_level(logging.WARNING)
it is valid in script2.py
too.
import logging
from pyplelogger import Logger
log = Logger(__name__).build()
log.info("hogehoge")
#=> Nothing is pritted out
Change logger level
This is similier to changing default log level but this method changes log level for one logger.
Futhermore, the method to change default log level is a class method but, this method is a instance method, so it will only effect the instance.
import logging
from pyplelogger import Logger
log = Logger(__name__).set_log_level(logging.WARNING).build()
log.info("hogehoge")
#=> Nothing is pritted out
Change default format
You can change default format of every instance of Logger by this method. Pass string object describing logging format.
format = '%(levelname)s %(asctime)s %(message)s'
Logger.set_default_format(format)
log = Logger(__name__).build()
log.info("hogehoge")
This will print out this logs.
INFO 2018-11-28 18:11:15,109 hogehoge
Change format
Specify format in string. The default format is '%(levelname)s %(asctime)s %(module)s.py:%(funcName)s in line %(lineno)d: %(message)s'
.
from pyplelogger import Logger
log_before = Logger(__name__).build()
log_after = Logger(__name__ + "after").set_format('%(levelname)s %(message)s').build()
log_before.info("before")
log_after.info("after")
Here is the output.
INFO 2018-11-26 23:11:15,109 test.py:main in line 4: before
INFO after
With Argparse
This has good integration with argparse, a library for creating cli tool.
Verbose flag
This is just a simple example of the verbose flag.
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--verbose', '-v', action='count')
args = parser.parse_args()
Now you can get number of v
flags like
-v
: 1-vvv
: 3
You can convert to log level by Using IncrementalLoggerLevel
IntEnum Class.
# 1 is debug level
count = 1
log_level = IncrementalLoggerLevel.convert_logger_level(1)
log = Logger().set_log_level(log_level)
log.DEBUG("hoge")
Then you will see
#=>
DEBUG 2018-11-26 23:11:15,109 test.py:main in line 3: hoge
The default level of logging is INFO
in current version.
level | number | count |
---|---|---|
CRITICAL | 50 | - |
ERROR | 40 | - |
WARNING | 30 | - |
INFO | 20 | 0 |
DEBUG | 10 | 1 |
NOTSET | 0 | 2 |
To contribute
We welcome for your contribution.
- Fork this project
- Run
just
to install dependencies. - Give us a pull request
Member
- KeisukeYamashita: Maintainer and creater
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 Distributions
Built Distribution
File details
Details for the file pyplelogger-0.1.8-py3-none-any.whl
.
File metadata
- Download URL: pyplelogger-0.1.8-py3-none-any.whl
- Upload date:
- Size: 6.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6bdc16e314ed63c0e6ab6b1d010491cd82d04aa40db0033e8cc794b47ba68eee |
|
MD5 | 9e73e14a6d846c960781f0b4abd19152 |
|
BLAKE2b-256 | 024cd6cc5e47f98be6dad1b3cef874aa47b1308fc94466ccfb7bafa9c3cc88cf |