Code Monkey home page Code Monkey logo

best_python_logger's Introduction

Best Python logger

(For the record, I discovered loguru and if it fulfill my needs I'm gonna abandon this project)

This is the best python logger. It doesn't require additional packages. It is so great that deserved its own repo.

Stolen and modified from here and here

Usage

Simplest usage

from best_python_logger import get_logger
logger = get_logger(__name__)
# Use the variable __name__ so the logger will print the file's name also

logger.debug("<some important rocket science phrase>")
logger.info("Awesome!")
logger.error("Ok, we are in trouble.")
logger.critical("Damn blinks that doesn't work on most IDEs built-in terminals..")

Master usage

import time
from best_python_logger import get_logger
import logging

test_logger = get_logger(name=__name__,
                         filename="logs/test/dirs/test.log",
                         when="S",
                         interval=3,
                         backup_count=3,
                         visible_stream=True,
                         compress=True,
                         auto_colorized=True,
                         custom_format=None,
                         only_visible_stream_auto_colorized=True,
                         level=logging.INFO)

count = 0
while True:
    count += 1
    time.sleep(0.001)
    test_logger.debug("hi, " + str(count))

README of one of the sources

(I though it would be useful for some people to read the comments of the owner of safe_logger package, from which I got the concurrency fix part)

## Description

This handler is designed to provide correct log files rotation when multiple processes writing to file.

Heavilly tested on production systems with up to 50 writers.

**Caveat**: this logger has been extracted from other system, so can have issues cause by copy-pasting

## Pitfalls

* Naive aproach fails because concurrent processes do independent rollovers and finally you will have zero-lenght log file: so we need locking
* Naive locking fails when you have some processes that not write often and them can rotate you file in case you archive old, or etc.: so we need check file that want to move by compare inodes
* Naive approach to start handler have issues, when you open file that in rollover - you can write data to rotated file: so we need locking on open file

best_python_logger's People

Contributors

herzog0 avatar teodoro-mendes-brainweb 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.