Skip to content

A small wrapper around python logging module which can easily format and write logs to file.

License

Notifications You must be signed in to change notification settings

swanlegion/pylogging

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pylogging

A simple python logger which writes logs to disk with some default configs.

Compatible with:

Python 2.7 and 3.5

Current stable version:

0.1.0

Installation:

Install using pip

pip install git+https://github.com/ansrivas/pylogging.git --upgrade

Install by adding to requirements.txt of your project

  • Add the following lines to your requirements.txt file.
    git+https://github.com/ansrivas/pylogging.git

  • Install all packages in your requirements.txt file by running the command:
    $ pip install -r requirements.txt

Install by adding to setup.py of your project

  • Add the following to the install_requires parameter of your setup function:
    install_requires=['pylogging==0.1.0'],

  • Add the following to the dependency_links parameter of your setup function:
    dependency_links=['https://github.com/ansrivas/pylogging/tarball/master#egg=pylogging-0.1.0'],

  • Install your project along with pylogging by running the command:
    python setup.py install

Usage:

  • setup_logger sets up the global logger with the provided settings. After calling it once, simply import logging and create a logger for that module logger = logging.getLogger(__name__) and use it at shown below.
from pylogging import HandlerType, setup_logger
import logging

logger = logging.getLogger(__name__)

if __name__ == '__main__':
    setup_logger(log_directory='./logs', file_handler_type=HandlerType.ROTATING_FILE_HANDLER, allow_console_logging=True)

    logger.error("Error logs")
    logger.debug("Debug logs")
    logger.info("Info logs")

Important arguments to setup_logger function:

log_directory:str          directory to write log files to. Applicable only when `allow_file_logging` = True
file_handler_type:         object of logging handler from HandlerType class. Applicable only when `allow_file_logging` = True
allow_console_logging:bool Turn off/on the console logging.
allow_file_logging:bool    Turn off/on if logs need to go in files as well.
backup_count:int           Number of files to backup before rotating the logs.
max_file_size_bytes:int    Size of file in bytes before rotating the file. Applicable only to ROTATING_FILE_HANDLER.
when_to_rotate:str         Duration after which a file can be rotated. Applicable only to TIME_ROTATING_FILE_HANDLER
                           Accepts following values:
                            'S'	Seconds
                            'M'	Minutes
                            'H'	Hours
                            'D'	Days
                            'W0'-'W6'	Weekday (0=Monday)
                            'midnight'	Roll over at midnight
change_log_level:dict      A dictionary of handlers with corresponding log-level ( for eg. {'requests':'warning'} )

About

A small wrapper around python logging module which can easily format and write logs to file.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Python 94.9%
  • Makefile 5.1%