Skip to content

Instantly share code, notes, and snippets.

@josefdlange
Last active October 10, 2015 00:19
Show Gist options
  • Select an option

  • Save josefdlange/0101ac396f1e0e14e9f5 to your computer and use it in GitHub Desktop.

Select an option

Save josefdlange/0101ac396f1e0e14e9f5 to your computer and use it in GitHub Desktop.
Short, sweet, to-the-point logging in a Python application.
import logging
import logging.handlers
"""
Call me once at initialization of your application.
@param level A level from the logging package. You can also
pass in common levels as strings, I believe ('INFO', 'DEBUG', etc)
"""
def setup_logging(level):
l = logging.getLogger('my_application')
syslog = logging.handlers.SysLogHandler(
address=SYSLOG_LOCATION,
facility=logging.handlers.SysLogHandler.LOG_DAEMON
)
syslog.formatter = logging.Formatter(
fmt='{0}: [%(name)s|%(lineno)d]: %(message)s'.format('MyApplication')
)
l.addHandler(syslog)
"""
Call me at the top of your module and set to a module-level attribute.
Use throughout the module where needed.
@param name A name to give the logger. Typically it's __name__ from
the calling module, but you can use anything.
"""
def get_logger(name=None):
# put anything you want.
full_name = "{0}.{1}".format('my_application', name or 'unknown_module')
return logging.getLogger(full_name)
# End
@josefdlange

Copy link
Copy Markdown
Author

Ideally, values like 'my_application' and 'MyApplication' would be configured in some configuration-declaring module, and you'd be passing them by reference into where necessary, not actually typing them out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment