lggr

Python Logging For Humans
Download

lggr Ranking & Summary

Advertisement

  • Rating:
  • License:
  • MIT/X Consortium Lic...
  • Price:
  • FREE
  • Publisher Name:
  • Peter Downs
  • Publisher web site:
  • https://github.com/peterldowns/

lggr Tags


lggr Description

Have you ever tried to do anything with the Python logging module?I have. I didn't like it at all. The API was very confusing. Instead of dealing with all of its intricacies, I decided to roll my own.I've been inspired by dabeaz's presentation on coroutines and Kenneth Reitz's presentation on better python libraries.Installpip install lggrHow does it work?Create a logger object.import lggrd = lggr.Lggr()Add a coroutine (or any function or object with send and close methods) to consume log messages. lggr includes some default ones:- lggr.Printer() writes to stdout- lggr.StderrPrinter() writes to stderr- lggr.Printer(filepath) opens a file at filepath and writes to that.- lggr.SocketWriter(host, port) writes to a network socket- lggr.Emailer(recipients) sends emails- lggr.GMailer(recipients, gmail_username, gmail_password, subject="optional") also sends emails, but does it from Gmail which is way sexier than doing it from your own server.You can choose to add different coroutines to different levels of logging. Maybe you want to receive emails for all of your critical messages, but only print to stderr for everything else.d.add(d.ALL, lggr.Printer()) # d.ALL is a shortcut to add a coroutine to all levelsd.add(d.CRITICAL, lggr.Emailer("peterldowns@gmail.com"))Do some logging.d.info("Hello, world!")d.warning("Something seems to have gone {desc}", {"desc":"amuck!"})d.critical("Someone {} us {} the {}!", "set", "up", "bomb")d.close() # stop loggingWhat kind of information can I log?Anything you want. Log messages are created using str.format, so you can really create anything you want. The default format includes access to the following variables:- levelname = level of logging as a string ("INFO")- levelno = level of logging as an integer (0)- pathname = path to the file that the logging function was called from (~/test.py)- filename = filename the logging function was called from (test.py)- module = module the logging function was called from (in this case, None)- exc_info = execution information, either passed in or sys.info()- stack_info = stack information, created if the optional inc_stack_info argument is True (it defaults to False if not explicitly passed) or the logging function is called with instance functions critical, debug, or error.- lineno = the line number- funcname = the function name- code = the exact code that called the logging function- codecontext = surrounding 10 lines surrounding code- process = current process id- processname = name of the current process, if multiprocessing is available- asctime = time as a string (from time.asctime())- time = time as seconds from epoch (from time.time())- threadid = the thread id, if the threading module is available- threadname = the thread name, if the threading module is available- messagefmt = the format string to be used to create the log message- logmessage = the user's formatted message- defaultfmt = the default format of a log messageIf you want to use any extra information, simply pass in a dict with the named argument extra:>>> d.config = '{name} sez: {logmessage}'>>> d.info("This is the {}", "message", extra={"name":"Peter"})Peter sez: This is the messageA stack_info examplestack_info is cool because it lets you do really helpful tracebacks to where exactly your logging function is being called. For example, with some logger d, I could run the following:d.config = '{asctime} ({levelname}) {logmessage}\nIn {pathname}, line {lineno}:\n{codecontext}'def outer(a): def inner(b): def final(c): d.critical("Easy as {}, {}, {}!", a, b, c) return final return innerouter(1)(2)(3)output:Mon Apr 2 23:31:22 2012 (CRITICAL) Easy as a, b, c!In test.py, line 29:d.config = '{asctime} ({levelname}) {logmessage}\nIn {pathname}, line {lineno}:\n{codecontext}'def outer(a): def inner(b): def final(c):> d.critical("Easy as {}, {}, {}!", a, b, c) return final return innerouter(1)(2)(3)Product's homepage


lggr Related Software