Ir al contenido principal

This is my blog, more about me at marianoguerra.github.io

🦋 @marianoguerra.org 🐘 @marianoguerra@hachyderm.io 🐦 @warianoguerra

mlog, logging for human beings

siempre empiezo un modulo y empiezo a poner prints, pienso "tendría que usar logging", pero el hecho de no poder recordar como se configura un logger y tanta verbositud hace que siempre termine usando prints.

peor cuando tengo que cambiar a logging tengo que leer la documentación y termino haciendo copy paste del código de los ejemplos.

para eso cree un wrapper que me hace la vida mas simple en mis casos de uso simples.

pego acá parte del readme del modulo (en ingles por fiaca a traducirlo).

si les gusta el código esta acá:

https://github.com/marianoguerra/doodles/tree/master/python

si ven algún error, problema o tienen alguna observación para hacer son bienvenidos, nunca termino de entender el modelo de logging ya sea en python o en java (el único que entiendo es console.log y console.error de js :D)

acá va un ejemplo de uso:



import time

import mlog

# create a logger for the console with default logging level, default format
# and default everything (but nice defaults)
log = mlog.console()
# log some stuff
log.info("hey!")
log.warn("warning...")
log.error("something went wrong! (no it didn't just testing log.error)")

# add a file handler to the logger we where using (see how I pass the logger?)
# if I don't pass the logger instance I get a brand new logger that is
# unrelated to the last one, but in this case I add a handler to the one I have
mlog.file("mlog.log", logger=log)

# log some more stuff
log.debug("I'm on a file now!")
log.info("of course I'm on both")

# who wants file loggers without rotation?
# I give you a nice notation to specify the rotation policy
# add this handler to the logger I already have
mlog.file("every-minute.log", when=mlog.every(1, mlog.MINUTE), logger=log)

log.info("im on a file that rotates every minute")

# give time so it rotates
time.sleep(65)

log.info("65 seconds passed!")

# this is a new logger not related with the others
# change the module name so it's not in the same hierarchy
# as the other ones
other_log = mlog.file("unrelated.log", name="test_mlog_1")

# log something
other_log.info("unrelated logger is unrelated")
running it produces the following output (and files):

$ python test_mlog.py

[2011-08-12 11:44:30,491] test_mlog INFO: hey!
[2011-08-12 11:44:30,491] test_mlog WARNING: warning...
[2011-08-12 11:44:30,491] test_mlog ERROR: something went wrong! (no it didn't just testing log.error)
[2011-08-12 11:44:30,492] test_mlog INFO: of course I'm on both
[2011-08-12 11:44:30,492] test_mlog INFO: im on a file that rotates every minute
[2011-08-12 11:45:35,491] test_mlog INFO: 65 seconds passed!

$ ls
README.rst mlog.log test_mlog.py
every-minute.log mlog.py unrelated.log
every-minute.log.2011-08-12_11-44 mlog.pyc

$ cat mlog.log
[2011-08-12 11:44:30,491] test_mlog DEBUG: I'm on a file now!
[2011-08-12 11:44:30,492] test_mlog INFO: of course I'm on both
[2011-08-12 11:44:30,492] test_mlog INFO: im on a file that rotates every minute
[2011-08-12 11:45:35,491] test_mlog INFO: 65 seconds passed!

$ cat every-minute.log
[2011-08-12 11:45:35,491] test_mlog INFO: 65 seconds passed!

$ cat every-minute.log.2011-08-12_11-44
[2011-08-12 11:44:30,492] test_mlog INFO: im on a file that rotates every minute

$ cat unrelated.log
[2011-08-12 11:45:35,495] test_mlog_1 INFO: unrelated logger is unrelated