• This repository has been archived on 28/Nov/2022
  • Stars
    star
    1,028
  • Rank 43,235 (Top 0.9 %)
  • Language
    Python
  • License
    MIT License
  • Created almost 7 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Robust and effective logging for Python 2 and 3.

logzero

Build status for master branch Documentation Status Latest version on PyPi Anaconda-Server Badge Downloads

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.
  • JSON logging (with integrated python-json-logger)
  • Pretty formatting, including level-specific colors in the console.
  • No dependencies
  • 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 and processes).
  • 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.

Installation:

python -m pip install logzero

Example Usage

from logzero import logger

logger.debug("hello")
logger.info("info")
logger.warning("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)

# JSON logging
import logzero
logzero.json()

logger.info("JSON test")

# Start writing into a logfile
logzero.logfile("/tmp/logzero-demo.log")

# Set a minimum loglevel
logzero.loglevel(logzero.WARNING)

This is the output:

demo-output

Note: You can find more examples in the documentation: https://logzero.readthedocs.io

JSON logging

JSON logging can be enabled for the default logger with logzero.json(), or with setup_logger(json=True) for custom loggers:

>>> logzero.json()
>>> logger.info("test")
{"asctime": "2020-10-21 10:42:45,808", "filename": "<stdin>", "funcName": "<module>", "levelname": "INFO", "levelno": 20, "lineno": 1, "module": "<stdin>", "message": "test", "name": "logzero_default", "pathname": "<stdin>", "process": 76179, "processName": "MainProcess", "threadName": "MainThread"}

>>> my_logger = setup_logger(json=True)
>>> my_logger.info("test")
{"asctime": "2020-10-21 10:42:45,808", "filename": "<stdin>", "funcName": "<module>", "levelname": "INFO", "levelno": 20, "lineno": 1, "module": "<stdin>", "message": "test", "name": "logzero_default", "pathname": "<stdin>", "process": 76179, "processName": "MainProcess", "threadName": "MainThread"}

The logged JSON object has these fields:

{
  "asctime": "2020-10-21 10:43:40,765",
  "filename": "test.py",
  "funcName": "test_this",
  "levelname": "INFO",
  "levelno": 20,
  "lineno": 9,
  "module": "test",
  "message": "info",
  "name": "logzero",
  "pathname": "_tests/test.py",
  "process": 76204,
  "processName": "MainProcess",
  "threadName": "MainThread"
}

Exceptions logged with logger.exception(e) have these additional JSON fields:

{
  "levelname": "ERROR",
  "levelno": 40,
  "message": "this is a demo exception",
  "exc_info": "Traceback (most recent call last):\n  File \"_tests/test.py\", line 15, in test_this\n    raise Exception(\"this is a demo exception\")\nException: this is a demo exception"
}

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

Installation

Install logzero with pip:

python -m pip install logzero

Here's how you setup a virtualenv and download and run the demo:

# Create and activate a virtualenv in ./venv/
python3 -m venv venv
. venv/bin/activate

# Install logzero
python -m pip install logzero

# Download and run demo.py
wget https://raw.githubusercontent.com/metachris/logzero/master/examples/demo.py
python demo.py

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

Alternatively, if you use the Anaconda distribution:

$ conda config --add channels conda-forge
$ conda install logzero

You can also install logzero from the public Github repo:

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

Contributors


Development

Getting started

$ git clone https://github.com/metachris/logzero.git
$ cd logzero

# Activate virtualenv
$ python3 -m venv venv
$ . venv/bin/activate

# Install main and dev dependencies
$ pip install -e .
$ pip install -r requirements_dev.txt

# Run the tests
$ make test

# Run the linter
$ make lint

# Generate the docs (will auto-open in Chrome)
$ make docs

# You can enable watching mode to automatically rebuild on changes:
$ make servedocs

To test with Python 2.7, you can use Docker:

docker run --rm -it -v /Users/chris/stream/logzero:/mnt python:2.7 /bin/bash

Now you have a shell with the current directory mounted into /mnt/ inside the container.

Notes


Changelog

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

Feedback

All kinds of feedback and contributions are welcome.

logo

More Repositories

1

pdfx

Extract text, metadata and references (pdf, url, doi, arxiv) from PDF. Optionally download all referenced PDFs.
Python
1,002
star
2

typescript-boilerplate

A modern TypeScript project setup, for Node.js and browsers
TypeScript
586
star
3

vue-highlightjs

Syntax highlighting with highlight.js for Vue.js 2.x
JavaScript
336
star
4

RPIO

RPIO is a GPIO toolbox for the Raspberry Pi.
C
325
star
5

appengine-boilerplate

Boilerplate setup for App Engine with html5-boilerplate 2.0, OpenID, memcache, user preferences, and more
JavaScript
185
star
6

flashbotsrpc

Golang client for Flashbots Relay, mev-geth and standard Ethereum JSON-RPC API endpoints
Go
136
star
7

eth-go-bindings

Go bindings for Ethereum smart contracts: ERC20, ERC165 and ERC721, ERC777, ERC1155
Go
87
star
8

py2app

Fork of the original py2app from https://bitbucket.org/ronaldoussoren/py2app
Python
72
star
9

flashbots

Flashbots utilities in Go: Blocks & Transactions API, and tools to spot bundle and block irregularities
Go
42
star
10

flashbots-ethers-example

Flashbots Ethers TypeScript example for Node.js and browser
TypeScript
37
star
11

most-simple-ajax-chat-ever

Fun project from back in 2006 ^^
HTML
30
star
12

micropython-ctl

TypeScript library for talking to MicroPython devices from websites/webapps, Node.js and Electron apps
TypeScript
27
star
13

raspberrypi-utils

Collection of utilities for the Raspberry Pi
Python
21
star
14

go-ethutils

Go helpers for working with Ethereum.
Go
20
star
15

feedmailer

RSS to Email Webapp (Python, AppEngine)
Python
17
star
16

retrofit2-samples

Samples for Retrofit 2.0
Java
15
star
17

wpscanner

Wordpress security scanner written in Python
Python
13
star
18

micropython-captiveportal

Minimal async captive portal for MicroPython (compatible with uasyncio v3/MicroPython 1.13+ as well as earlier versions)
Python
11
star
19

eth-was-tx-uncled

Go
11
star
20

django-boilerplate

Django Boilerplate
Python
10
star
21

binary-serializer

Minimalistic binary serialization protocol (a la protocol buffers) with Python and Java implementations.
Python
9
star
22

rfid-music-player

A simple RFID music player for kids (runs on a Raspberry Pi)
Python
9
star
23

coz-python-smart-contract-workshop

8
star
24

raspberrypi-django

Raspberry Pi Django Setup
Python
8
star
25

python-posix-daemon

Simple and efficient Python daemon framework based on Sander Marechal's code.
Python
7
star
26

android-bluetooth-spp

Bluetooth Serial Communication with Android + PC/Arduino
Python
7
star
27

jekyll-boilerplate

CSS
7
star
28

metalab-git-workshop

Repository for the GIT Workshop at Metalab
6
star
29

android-apprater

Prompt engaged users to rate your app in the Android market
Java
5
star
30

flashbots-tx-telegram-bot

Telegram bot to notify about failed 0-gas and flashbots tx
Go
3
star
31

GridGrouper

Group similar members close to each other in a grid
Python
2
star
32

pdfx-gui

PyQt/QML Gui for PDFx
Python
2
star
33

tornado-boilerplate

Boilerplate for the tornado web framework
JavaScript
2
star
34

androidsnippets.com

Rewrite of androidsnippets.com for relaunch
Python
2
star
35

micropython-minimal-webserver-asyncio3

Minimal MicroPython webserver / TCP connection handler using uasyncio v3 (MicroPython 1.13+), with fallback for earlier versions of uasyncio / MicroPython
Python
2
star
36

photoblog

Python
2
star
37

infomatic

Software for Picture in Picture Mode with a Raspberry Pi
Python
1
star
38

photoblog-styles

1
star
39

pifm

C
1
star
40

HN-Reduce

Chrome extension to remove stories on HN
JavaScript
1
star
41

Climbing-Log

Java
1
star
42

clicklooper

Raspberry Pi audio player which jumps to next album on mouse click
Python
1
star
43

metachris.github.com

JavaScript
1
star
44

raspberrypi-tools

Shell
1
star
45

python-tornado-tcp-boilerplate

Python
1
star
46

python-raspberrypi-boilerplate

Boilerplate Python Project for the Raspberry Pi
Python
1
star
47

Snapshotter

web.py app that takes snapshots from a website and stores them in S3
Python
1
star
48

pypi-downloads-tracker

Python
1
star
49

csspivot

JavaScript
1
star
50

boilerplates

various boilerplates
Vim Script
1
star
51

neo-cli-ci

Continuous integration setup for neo-cli
Shell
1
star