Giter VIP home page Giter VIP logo

sharklog's Introduction

sharklog

Python logging helper.

PyPI License PyPI Version

Quick Start

  • Install sharklog:
python -m pip install sharklog
  • Use in standalone script:
# standalone.py
from sharklog import logging

logging.init(debug=True)    # init all loggers with level=logging.DEBUG
# or logging.init(level=logging.DEBUG)
logging.debug("debug message")
logging.info("info message")
logging.warning("warning message")
logging.error("error message")

If you want to change logging level for a module, you can set it in the module by:

from sharklog import logging
logging.getLogger().setLevel(logging.DEBUG)

or set it outside the module by specifying the logger name:

from sharklog import logging
logging.getLogger("module_name").setLevel(logging.DEBUG)

The default format of log messages is:

"[%(levelname)s]: %(message)s [%(asctime)s](%(filename)s:%(lineno)d)"

Usage in Package Development

Now I assume your file structure is like this:

--- parent_package
    |--- __init__.py
    |--- parent_module.py
    |--- logger.py
    |--- sub_package
        |--- __init__.py
        |--- sub_module.py
  • First, you can add NullHandler to the root logger in parent_package/__init__.py, which logger named parent_package:
# parent/__init__.py
from sharklog import logging

logging.getLogger().addHandler(logging.NullHandler())

This is mentioned in the Python Logging HOWTO to identify the logger's default behavior.

  • Then, use logging in your package, they will be prefixed with the logger name parent_package.:
# parent_module.py which is placed under package `parent_package`
from sharklog import logging

logger = logging.getLogger()    # the logger name will be `parent_package.parent_module`

logger.debug("debug message")
logger.info("info message")
logger.warning("warning message")
logger.error("error message")

If you already using builtin logging module, you can use sharklog as a drop-in replacement.

Just change import logging into from sharklog import logging. Then you can use logging as usual:

# sub_module.py
from sharklog import logging

# these log messages will be prefixed with the logger name `xxxpackage.xxmodule.module_name`
# here, as an example, the logger name will be `parent_package.sub_package.sub_module`
logging.debug("debug message")
logging.info("info message")
logging.warning("warning message")
logging.error("error message")
  • Finally, you can set the logging level for the package in the main script which using the package:
# main.py
from sharklog import logging

from parent_package import parent_module
from parent_package.sub_package import sub_module

if __name__ == "__main__":
    logging.init(debug=True)    # init all loggers with level=logging.DEBUG
    # or logging.init(level=logging.DEBUG)
    # logging inside the package will use the level set here

Or if you want to change the logging level for a specific module, you can set it in the module by:

from sharklog import logging
logging.getLogger().setLevel(logging.WARNING)

sharklog's People

Contributors

bardreamaster avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.