initial commit
This commit is contained in:
commit
c352531f0c
|
@ -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
|
|
@ -0,0 +1,3 @@
|
|||
# file GENERATED by distutils, do NOT edit
|
||||
setup.py
|
||||
sanelogging/__init__.py
|
|
@ -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.
|
|
@ -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
|
|
@ -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",
|
||||
],
|
||||
)
|
Loading…
Reference in New Issue