Giter Site home page Giter Site logo

yuejd / logzero Goto Github PK

View Code? Open in Web Editor NEW

This project forked from metachris/logzero

1.0 1.0 0.0 959 KB

Robust and effective logging for Python 2 and 3.

Home Page: https://logzero.readthedocs.io

License: MIT License

Makefile 5.71% Python 94.29%

logzero's Introduction

logzero

Latest version on PyPi

Build status for master branch

Documentation Status

Updates

Robust and effective logging for Python 2 and 3.

Logo

Features

  • Easy logging to console and/or (rotating) file.
  • Provides a fully configured standard Python logger object.
  • Pretty formatting, including level-specific colors in the console.
  • Windows color output supported by colorama
  • Robust against str/bytes encoding problems, works with all kinds of character encodings and special characters.
  • Multiple loggers can write to the same logfile (also across multiple Python files).
  • Global default logger with logzero.logger and custom loggers with logzero.setup_logger(..).
  • Compatible with Python 2 and 3.
  • All contained in a single file.
  • Licensed under the MIT license.
  • Heavily inspired by the Tornado web framework.

Demo output in color

Example Usage

from logzero import logger

logger.debug("hello")
logger.info("info")
logger.warn("warn")
logger.error("error")

# This is how you'd log an exception
try:
    raise Exception("this is a demo exception")
except Exception as e:
    logger.exception(e)

Adding a rotating logfile is that easy:

import logzero
from logzero import logger

# Setup rotating logfile with 3 rotations, each with a maximum filesize of 1MB:
logzero.logfile("/tmp/rotating-logfile.log", maxBytes=1e6, backupCount=3)

# Log messages
logger.info("This log message goes to the console and the logfile")

Here are more examples which show how to use logfiles, custom formatters and setting a minimum loglevel:

import logging
import logzero
from logzero import logger

# This log message goes to the console
logger.debug("hello")

# Set a minimum log level
logzero.loglevel(logging.INFO)

# Set a logfile (all future log messages are also saved there)
logzero.logfile("/tmp/logfile.log")

# You can also set a different loglevel for the file handler
logzero.logfile("/tmp/logfile.log", loglevel=logging.ERROR)

# Set a rotating logfile (replaces the previous logfile handler)
logzero.logfile("/tmp/rotating-logfile.log", maxBytes=1000000, backupCount=3)

# Disable logging to a file
logzero.logfile(None)

# Set a custom formatter
formatter = logging.Formatter('%(name)s - %(asctime)-15s - %(levelname)s: %(message)s');
logzero.formatter(formatter)

# Log some variables
logger.info("var1: %s, var2: %s", var1, var2)

Take a look at the documentation for more information and examples:

Installation

Install logzero with pip:

$ pip install -U logzero

If you don't have pip installed, this Python installation guide can guide you through the process.

You can also install logzero from the public Github repo:

$ git clone https://github.com/metachris/logzero.git
$ cd logzero
$ python setup.py install

On openSUSE you can install the current version from repos: python2-logzero, python3-logzero. In the newest openSUSE release you can install it with zypper: sudo zypper in python2-logzero.

Changelog

See the changelog here: https://github.com/metachris/logzero/blob/master/HISTORY.rst

Feedback

All kinds of feedback and contributions are welcome.

logzero's People

Contributors

metachris avatar pyup-bot avatar delirious-lettuce avatar sebix avatar

Stargazers

Robert avatar

Watchers

James Cloos 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.