Skip to content

Convenient wrapper for logging python applications with options to add color, select logging style, rotate files by size and time, etc.

License

Notifications You must be signed in to change notification settings

srtamrakar/python-logger

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NeatLogger

Convenient wrapper for logging python applications with options to add color, select logging style, rotate files by size and time, etc.

Output from demo.py: demo logs

Install with pip

$ pip install NeatLogger

Usage

  1. Import the library.

    from NeatLogger import Log
  2. Create an instance.

    log = Log()

    Arguments (all are optional):

    • project_name: Project name, which would serve as the logger's name (if specified), and the prefix for log filenames.
    • log_level: Level for logging. Choices:
      • "critical"
      • "error"
      • "warning"
      • "info"
      • "debug"
      • "notset"
    • assign_logger_name: Flag to decide whether to assign project_name as the name to the logger.
    • formatter: Logging formatter. Choices:
      • an instance of logging.Formatter
      • "json"
      • "apache"
    • log_to_stdout: Flag to decide whether to display the logs in stdout.
    • log_to_file: Flag to decide whether to store the logs in a file.
    • log_dir: Directory where the log files are stored.
    • log_file_suffix: Suffix for the log filenames. Ignored if rotate_file_by_time=True . Choices:
      • "S": %Y-%m-%d_%H-%M-%S is appended to the filename.
      • "M": %Y-%m-%d_%H-%M-00
      • "H": %Y-%m-%d_%H-00-00
      • "D": %Y-%m-%d
    • rotate_file_by_size: Flag to decide whether to rotate the log files by size.
    • rotating_file_max_size_bytes: Size (in bytes) threshold to rollover the log files.
    • rotate_file_by_time: Flag to decide whether to rotate the log files by time. Ignores log_file_suffix.
    • rotation_period: Rotation period for the log files. Choices:
      • "S": log file rollovers every second. Ignores rotation_time.
      • "M": log file rollovers every minute. Ignores rotation_time.
      • "H": log file rollovers every hour. Ignores rotation_time.
      • "D": log file rollovers every day. Ignores rotation_time.
      • "MIDNIGHT": log file rollovers at midnight, or at rotation_time if specified.
      • "W0": log file rollovers on weekday 0 i.e. Monday, at rotation_time if specified.
      • "W1"
      • "W2"
      • "W3"
      • "W4"
      • "W5"
      • "W6"
    • rotation_interval: Intervals of rotation period to rollover the log files. Ignored if rotation_time is a weekday.
    • rotation_time: Time of the day to rollover the log file when rotation_period = "MIDNIGHT" or a weekday.
    • rotating_file_backup_count: Number of old files to be retained.
    • use_utc: Flag to decide whether to use UTC in the log timestamp and filenames.
    • colors_to_stdout: Flag to decide whether to have colorful log. Only works for log_to_stdout=True and log_formatter="apache".
    • ignore_log_attribute_list: List of log attributes to be ignored in the log. By default, some attributes are ignored. If all the attributes are desired, use ignore_log_attribute_list=list().

    ⚠️ If more than 1 of the following are set to True, only one of them is implemented. Their priority follows the order:

    1. rotate_file_by_size
    2. rotate_file_by_time
    3. log_to_file
  3. Get a logger and start logging.

    logger = log.get_logger()
    logger.info("Testing 1 2 3 ...")

Author

© 2021, Samyak Tamrakar.

About

Convenient wrapper for logging python applications with options to add color, select logging style, rotate files by size and time, etc.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages