Make logging simple, log even exception that you forgot to catch
Project description
Logger_tt
Make configuring logging simpler and log even exceptions that you forgot to catch.
Usage:
Install: pip install logger_tt
In the most simple case, add the following code into your main python script of your project:
from logger_tt import setup_logging
setup_logging()
Then from any of your modules, you just need to get a logger and start logging.
from logging import getLogger
logger = getLogger(__name__)
logger.debug('Module is initialized')
logger.info('Making connection ...')
This will provide your project with the following default log behavior:
-
log file: Assume that your
working directoryisproject_root, log.txt is stored at yourproject_root/logs/folder. If the log path doesn't exist, it will be created. The log file is time rotated at midnight. A maximum of 15 dates of logs will be kept. This log file'slevelisDEBUG.
The log format is[%(asctime)s] [%(name)s %(levelname)s] %(message)swhere time is%Y-%m-%d %H:%M:%S.
Example:[2020-05-09 00:31:33] [myproject.mymodule DEBUG] Module is initialized -
console: log with level
INFOand above will be printed tostdoutof the console.
The format for console log is simpler:[%(asctime)s] %(levelname)s: %(message)s.
Example:[2020-05-09 00:31:34] INFO: Making connection ... -
urllib3logger: this ready-made logger is to silent unwanted messages fromrequestslibrary. -
rootlogger: if there is no logger initialized in your module, this logger will be used with the above behaviors. This logger is also used to log uncaught exception in your project. Example:
raise RecursionError
# log.txt
2020-05-31 19:16:01 ERROR [root] Uncaught exception
Traceback (most recent call last):
File "D:/MyProject/Echelon/eyes.py", line 13, in <module>
raise RecursionError
RecursionError
Config:
All configs are done through setup_logging function:
setup_logging(config_path="", log_path="", capture_print=False, strict=False, guess_level=False)
-
You can overwrite the default log path with your own as follows:
setup_logging(log_path='new/path/to/your_log.txt')
-
You can config your own logger and handler by providing either
yamlorjsonconfig file as follows:setup_logging(config_path='path/to/.yaml_or_.json')
Without providing a config file, the default config file with the above default log behavior is used. You could copy
log_conf.yamlorlog_conf.jsonshipped with this package to start making your version.Warning: To process
.yamlconfig file, you need topyyamlpackage:pip install pyyaml -
Capture stdout:
If you have an old code base with a lot of
print(msg)orsys.stdout.write(msg)and don't have access or time to refactor them into something likelogger.info(msg), you can capture thesemsgand log them to file, too.To capture only
msgthat is printed out byprint(msg), simply do as follows:setup_logging(capture_print=True)
Example:
print('To be or not to be') sys.stdout.write('That is the question')
# log.txt [2020-05-09 11:42:08] [PrintCapture INFO] To be or not to be
Yes,
That is the questionis not captured. Some libraries may directly usesys.stdout.writeto draw on the screen (eg. progress bar) or do something quirk. This kind of information is usually not useful for users. But when you do need it, you can capture it as follows:setup_logging(capture_print=True, strict=True)
Example:
sys.stdout.write('The plane VJ-723 has been delayed') sys.stdout.write('New departure time has not been scheduled')
# log.txt [2020-05-09 11:42:08] [PrintCapture INFO] The plane VJ-723 has been delayed [2020-05-09 11:42:08] [PrintCapture INFO] New departure time has not been scheduled
As you have seen, the log level of the captured message is
INFO. What if the code base prints something likeAn error has occurred. Abort operation.and you want to log it asError? Just addguess_level=Truetosetup_logging().setup_logging(capture_print=True, guess_level=True)
Example:
print('An error has occurred. Abort operation.') print('A critical error has occurred during making request to database')
# log.txt [2020-05-09 11:42:08] [PrintCapture ERROR] An error has occurred. Abort operation. [2020-05-09 11:42:08] [PrintCapture CRITICAL] A critical error has occurred during making request to databaseNote: Capturing stdout ignores messages of
blank line. That means messages like\n\nor(spaces) will not appear in the log. But messages that contain blank line(s) and other characters will be fully logged. For example,\nTo day is a beautiful day\nwill be logged as is.
Sample config:
-
Yaml format:
version: 1 disable_existing_loggers: False formatters: simple: format: "[%(asctime)s] [%(name)s %(levelname)s] %(message)s" datefmt: "%Y-%m-%d %H:%M:%S" brief: { format: "[%(asctime)s] %(levelname)s: %(message)s" datefmt: "%Y-%m-%d %H:%M:%S" handlers: console: class: logging.StreamHandler level: INFO formatter: simple stream: ext://sys.stdout error_file_handler: class: logging.handlers.TimedRotatingFileHandler level: DEBUG formatter: simple filename: logs/log.txt backupCount: 15 encoding: utf8 when: midnight loggers: urllib3: level: WARNING handlers: [console, error_file_handler] propagate: no root: level: DEBUG handlers: [console, error_file_handler]
2. Json format:
{
"version": 1,
"disable_existing_loggers": false,
"formatters": {
"simple": {
"format": "[%(asctime)s] [%(name)s %(levelname)s] %(message)s",
"datefmt": "%Y-%m-%d %H:%M:%S"
},
"brief": {
"format": "[%(asctime)s] %(levelname)s: %(message)s",
"datefmt": "%Y-%m-%d %H:%M:%S"
}
},
"handlers": {
"console": {
"class": "logging.StreamHandler",
"level": "INFO",
"formatter": "brief",
"stream": "ext://sys.stdout"
},
"error_file_handler": {
"class": "logging.handlers.TimedRotatingFileHandler",
"level": "DEBUG",
"formatter": "simple",
"filename": "logs/log.txt",
"backupCount": 15,
"encoding": "utf8",
"when": "midnight"
}
},
"loggers": {
"urllib3": {
"level": "ERROR",
"handlers": ["console", "error_file_handler"],
"propagate": false
}
},
"root": {
"level": "DEBUG",
"handlers": ["console", "error_file_handler"]
}
}
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file logger_tt-1.1.1.tar.gz.
File metadata
- Download URL: logger_tt-1.1.1.tar.gz
- Upload date:
- Size: 8.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cea02e071c7d6c9075af64af7f853f7e9da4be13a6d8a8b1efaf75a19eb2ec38
|
|
| MD5 |
607fb599a7b136d4ab58bbac58cf5005
|
|
| BLAKE2b-256 |
6668d11f45f82f977009eb0fc7d8a1e92302890ec045880f2326753050e846c4
|
File details
Details for the file logger_tt-1.1.1-py3-none-any.whl.
File metadata
- Download URL: logger_tt-1.1.1-py3-none-any.whl
- Upload date:
- Size: 7.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
43fbc42ffdeb24babedbdedcc72c2153bfc4680d1e3900e0f493cb55357b0197
|
|
| MD5 |
43da527f70be1fde8b3dffa32fcf4e13
|
|
| BLAKE2b-256 |
0d0bb2fbe819b3a962f0820fc1ced570a5b2486cb9f2f22af31965473cb42d6e
|