From c352531f0ca75ec9dcdfcdb4c2250a96ba92e7d9 Mon Sep 17 00:00:00 2001 From: Jeffrey Paul Date: Thu, 24 Mar 2016 07:19:45 +0100 Subject: [PATCH] initial commit --- .gitignore | 36 +++++++++++++++++++++++ LICENSE | 1 + MANIFEST | 3 ++ README.md | 50 +++++++++++++++++++++++++++++++ sanelogging/__init__.py | 65 +++++++++++++++++++++++++++++++++++++++++ setup.py | 16 ++++++++++ 6 files changed, 171 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 MANIFEST create mode 100644 README.md create mode 100644 sanelogging/__init__.py create mode 100644 setup.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ded6067 --- /dev/null +++ b/.gitignore @@ -0,0 +1,36 @@ +*.py[cod] + +# C extensions +*.so + +# Packages +*.egg +*.egg-info +dist +build +eggs +parts +bin +var +sdist +develop-eggs +.installed.cfg +lib +lib64 +__pycache__ + +# Installer logs +pip-log.txt + +# Unit test / coverage reports +.coverage +.tox +nosetests.xml + +# Translations +*.mo + +# Mr Developer +.mr.developer.cfg +.project +.pydevproject diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..7bbeaed --- /dev/null +++ b/LICENSE @@ -0,0 +1 @@ +This code is released into the public domain. diff --git a/MANIFEST b/MANIFEST new file mode 100644 index 0000000..2065495 --- /dev/null +++ b/MANIFEST @@ -0,0 +1,3 @@ +# file GENERATED by distutils, do NOT edit +setup.py +sanelogging/__init__.py diff --git a/README.md b/README.md new file mode 100644 index 0000000..c4c0982 --- /dev/null +++ b/README.md @@ -0,0 +1,50 @@ +# sanelogging + +## Sane defaults for python logging + +The python stdlib `logging` module is useful, flexible, and configurable. + +However, the maintainers reasonably have determined that python is an +application runtime and not an application. The default configuration +for the `logging` is not very useful, and this results in boilerplate. + +This is an opinionated module for the 90% case where you just want sane +defaults. (In effect, moving the boilerplate into PyPI.) + +# Other Stuff + +There are some convenience methods added, such as `panic` and `die` (c.f. +golang and perl). + +`notice` is additionally aliased to `info`, for those who forget that python +doesn't have a notice level (i.e. me). + +If you set the environment variable `LOG_TO_SYSLOG`, it will print out your +log messages on paper and mail them to you. + +# Usage + +``` +from sanelogging import log + +log.info("starting up!") + +log.error("something went wrong.") + +log.die("bailing out") # script exits + +``` + +Author +====== + +Jeffrey Paul <(sneak@sneak.berlin)[mailto:sneak@sneak.berlin]> + +(https://sneak.berlin)[https://sneak.berlin] + +(@sneakdotberlin)[https://twitter.com/sneakdotberlin] + +License +======= + +This code is released into the public domain. diff --git a/sanelogging/__init__.py b/sanelogging/__init__.py new file mode 100644 index 0000000..57a83a6 --- /dev/null +++ b/sanelogging/__init__.py @@ -0,0 +1,65 @@ +#!/usr/bin/env python +#234567891123456789212345678931234567894123456789512345678961234567897123456789 +# encoding: utf-8 + +import colorlog +import logging +import os +import sys + +# get root logger +log = logging.getLogger() +log.setLevel(logging.DEBUG) + +# console handler gets all log entries +ch = logging.StreamHandler() # defaults to sys.stderr + +log.addHandler(ch) + +formatstr = '%(asctime)s [%(levelname)-.4s] %(message)s' +colorFormatter = colorlog.ColoredFormatter( + '%(log_color)s' + formatstr +) + +formatter = logging.Formatter( + formatstr +) + + +log.notice = log.info + + +if sys.stdout.isatty(): + ch.setFormatter(colorFormatter) +else: + ch.setFormatter(formatter) + +# log to syslog if env var is setup +if os.environ.get('LOG_TO_SYSLOG',False): + + # default to UDP if no socket found + address = ('localhost', 514) + + from logging.handlers import SysLogHandler + + locations = [ + "/var/run/syslog", # osx + "/dev/log", # linux + "/var/run/log" # freebsd + ] + + for p in locations: + if os.path.exists(p): + address = p + + slh = SysLogHandler(address=address) + syslogFormatter = logging.Formatter('%(message)s') + slh.setFormatter(syslogFormatter) + log.addHandler(slh) + +def panic(msg): + log.critical(msg) + sys.exit(1) + +log.panic = panic +log.die = panic diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..55a6282 --- /dev/null +++ b/setup.py @@ -0,0 +1,16 @@ +from distutils.core import setup + +setup( + name='sanelogging', + version='1.0.0', + author='Jeffrey Paul', + author_email='sneak@datavibe.net', + packages=['sanelogging'], + url='http://pypi.python.org/pypi/sanelogging/', + license='LICENSE.txt', + description='Python logging for humans', + long_description=open('README.md').read(), + install_requires=[ + "colorlog", + ], +)