Easy Logger
Project description
easy-logger
Simple Easy baseline Logger. Creates a color stream logger or a basic rotating logger that can be used and called on throughout a project.
Usage
Configuration Values:
- name: Name of file can use built in __name__ or specifiy a name to call the logger instance.
- logName: filename for log.
- logDir: Directory location to store log.
- Can use built in utilitiy to find the default log location for the system you are on.
- level: Log Level. Can be one of the following Strings.
- NOTSET = 0
- DEBUG = 10
- INFO = 20
- WARN = 30
- WARNING = 30
- ERROR = 40
- CRITICAL = 50
- FATAL = 50
- setFile: Bool Value to set the logger to write out to file.
- stream: Bool Value to allow conosole output of log.
- maxBytes: Maximum Byte size before rotating log file.
- backupCount: Amount of backups to keep.
- setLog: Enables or creates the logging instance otherwise will not log anything.
import easy_logger
from easy_logger.utils import set_logdir
logger = easy_logger.RotatingLog(__name__,
logName="sample.log",
logDir=set_logdir("default"),
level="DEBUG",
stream=True,
setLog=True,
setFile=True,
maxBytes=10000,
backupCount=10)
log = logger.getLogger(__name__)
log.info(f"Writting out file to: {set_logdir('default')}")
log.info("This is an informational log message.")
log.debug("This is a debug log message.")
log.warning("This is a warning log message.")
log.critical("This is a critical log message.")
What this will look like in the console:
If written to file:
>>> cat ~/Library/Logs/sample.log
[2023-07-19 12:25:01,648] level=INFO name=__main__ fn=<ipython-input-1-e1c5bba1549c> ln=14 func=<module>: Writting out file to: ~/Library/Logs
[2023-07-19 12:25:01,650] level=INFO name=__main__ fn=<ipython-input-1-e1c5bba1549c> ln=15 func=<module>: This is an informational log message.
[2023-07-19 12:25:01,652] level=DEBUG name=__main__ fn=<ipython-input-1-e1c5bba1549c> ln=16 func=<module>: This is a debug log message.
[2023-07-19 12:25:01,658] level=WARNING name=__main__ fn=<ipython-input-1-e1c5bba1549c> ln=17 func=<module>: This is a warning log message.
[2023-07-19 12:25:01,667] level=CRITICAL name=__main__ fn=<ipython-input-1-e1c5bba1549c> ln=18 func=<module>: This is a critical log message.
If the name value is passed as a string instead of using the python file name the name will show up in the logs such as:
log = logger.getLogger("easy_logger")
Sample Log File Output Format
[2023-07-19 12:34:53,266] level=INFO name=easy_logger fn=<ipython-input-2-297430e75a74> ln=14 func=<module>: Writting out file to: ~/Library/Logs
[2023-07-19 12:34:53,268] level=INFO name=easy_logger fn=<ipython-input-2-297430e75a74> ln=15 func=<module>: This is an informational log message.
[2023-07-19 12:34:53,268] level=DEBUG name=easy_logger fn=<ipython-input-2-297430e75a74> ln=16 func=<module>: This is a debug log message.
[2023-07-19 12:34:53,269] level=WARNING name=easy_logger fn=<ipython-input-2-297430e75a74> ln=17 func=<module>: This is a warning log message.
[2023-07-19 12:34:53,269] level=CRITICAL name=easy_logger fn=<ipython-input-2-297430e75a74> ln=18 func=<module>: This is a critical log message.
NOTE: It is usually best practice when passing a logger around a large project to pick up the name of the file and the function for easier troubleshooting depending on what method you use.
Extended Functions
Application Log Directory:
You can set up a subdirectory for your application on a system using the system's default location by leveraging and extension function in the logDir value.
import easy_logger
from easy_logger.utils import set_logdir
logger = easy_logger.RotatingLog(__name__,
logName="sample.log",
logDir=set_logdir("extend", extend="ApplicationName"),
level="DEBUG",
stream=True,
setLog=True,
setFile=True,
maxBytes=10000,
backupCount=10)
log = logger.getLogger(__name__)
log.info(f"log directory set to {logger.settings.logDir}")
log.info("This is an informational log message.")
log.debug("This is a debug log message.")
log.warning("This is a warning log message.")
log.critical("This is a critical log message.")
Sample Application Log Directory Output
>>> cat ~/Library/Logs/ApplicationName/sample.log
[2023-07-28 13:03:48,438] level=INFO name=__main__ fn=<ipython-input-3-edd6755574f8> ln=1 func=<module>: log directory set to ~/Library/Logs/ApplicationName
[2023-07-28 13:03:48,443] level=INFO name=__main__ fn=<ipython-input-3-edd6755574f8> ln=2 func=<module>: This is an informational log message.
[2023-07-28 13:03:48,446] level=DEBUG name=__main__ fn=<ipython-input-3-edd6755574f8> ln=3 func=<module>: This is a debug log message.
[2023-07-28 13:03:48,450] level=WARNING name=__main__ fn=<ipython-input-3-edd6755574f8> ln=4 func=<module>: This is a warning log message.
[2023-07-28 13:03:48,452] level=CRITICAL name=__main__ fn=<ipython-input-3-edd6755574f8> ln=5 func=<module>: This is a critical log message.
Versions
v0.0.6
- Added color samples as defaults.
- Created a Color Console only.
- Updated reformat error to remove
[
,]
characters as it causes issues reading on some systems; replaced withlist()
. - Added ability to change Log Format for streaming.
v0.0.5
- Added a retry decorator.
- Added Error handler with ability to customize the error return value or ability to log out the error.
- BUGFIX issue with reformat Exception when added to a JSON object can become problematic so filtering out ugly formating from raised Exception.
v0.0.4
- Added utility to find OS Log directory.
- Added Splunk JSON data formatter.
v0.0.3
- Added Splunk basic string log out format.
- Added Splunk HEC JSON format for messaging.
Future Enhancements
- Use a standard or basic logger.
- Allow conversion of int/str types for log level.
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
Built Distribution
Hashes for py_easy_logger-0.0.6-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2c21f45af24e53df6d8c40b512c028a7b3d9e9db1b6db59dfceafd049be80ccc |
|
MD5 | 0d9eb300a2eb62de224354ffbc50f38f |
|
BLAKE2b-256 | 54ffea457dca04303d6be2150971f7a9c3c11f5f2bae36b7bc2e08e4bded6528 |