Giter Site home page Giter Site logo

python-logging-examples's Introduction

5 Things About Python Logging

This repository accompanies a talk given at the SFPython meet-up (here).

It is a collection of basic examples for how to use Python's logging API.

Loggers

  1. Simple logging: loggers/simple/main.py
  2. Using logger namespaces (or ancestry): loggers/namespaces/main.py
  3. Using __name__ for your namespaces: loggers/free_namespaces/main.py

Handlers

  1. Simple stream handlers: handles/streams/main.py
  2. Simple file handlers: handlers/files/main.py
  3. Handler levels vs. logger levels: handlers/levels/main.py
  4. Log messages over a network: handlers/network/main.py

Formatters

  1. Basic message formatting: formatters/message/main.py
  2. Date/Time formatting: formatters/date_time/main.py

BOOKMARK THESE SITES:

  1. Python string format cheat-sheet: https://pyformat.info/
  2. Python LogRecord attribute list: https://docs.python.org/2.7/library/logging.html#logrecord-attributes
  3. Python strftime format strings: https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior

Filters

  1. Basic filtering: filters/simple/main.py
  2. Injecting LogRecord attributes: filters/inject/main.py

Configuration

  1. Using basicConfig: configuration/basicConfig/main.py
  2. Using dictConfig: configuration/dictConfig/main.py
  3. Using fileConfig: configuration/fileConfig/main.py

Example Configuration

  1. .ini/.conf file: configuration/fileConfig/config.ini
  2. Native Python dict: configuration/dictConfig/main.py

Bonus - Python Log Config-Change Server

This is completely bonkers. Python has a built-in logging configuration update server.

This means you can change the logging config for an entire runtime on the fly, without restarting the process.

NOTE: This example uses Flask. If you want to run main.py, please be sure to pip install Flask first.

python-logging-examples's People

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.