GithubHelp home page GithubHelp logo

nmalkin / sclog Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 4 KB

Python logging helper with convenient defaults and colorized output for the logs

License: BSD 3-Clause "New" or "Revised" License

Python 100.00%
python python3 logging logger loggers colorized colorlog colorlogger

sclog's Introduction

sclog - simple colorized log

Rationale

Python has a pretty convenient logging framework built-in, which makes it super easy to get started:

import logging

logging.warning("That was easy")

Or, if you have a few different files in your project and want to distinguish between them:

import logging

logger = logging.getLogger(__name__)

logger.error("Uh oh, something bad happened")

But, there are a few annoyances.

First, as the documentation explains, debug and info messages aren't shown with the default log level. Want to add a quick logging.debug message to your scripts? You'll need to configure a log formatter first.

Second, the default log formatting is missing some information like the timestamp. The default output looks something like this:

WARNING:root:your warning message

...and if you want it to look differently, you again need a custom formatter.

This library

This library exists, to be honest, for my own convenience. Its purpose it to provide some useful (to me) defaults as a very light-weight layer on top of Python's default logging framework. Specifically, it:

  1. Sets the default log level to DEBUG
  2. Uses a formatter that includes the timestamp
  3. (Optionally) colorizes the output using colorlog (another task that normally requires creating a custom formatter)

Usage

The "API" for this library is meant to be maximally compatible with the built-in logging API. Like Python's logging module, it exposes a getLogger function that you call with a string (the name). This returns a standard Logger object that you use in a familiar way.

from sclog import getLogger

logger = getLogger(__name__)

logger.info("Hello, %s!", "world")

This outputs (in a nice green color):

2021-01-01 00:00:00:00,000 - __main__ - DEBUG - Hello, world!

Installation

pip install sclog
pip install colorlog  # optional, include if you want colors

You can also just copy the file into your own project.

sclog's People

Contributors

nmalkin avatar

Watchers

 avatar  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.