These are chat archives for astropy/astropy

1st
Sep 2016
Adam Ginsburg
@keflavich
Sep 01 2016 18:34
anyone familiar with the astropy-logging module know if it is possible to show the parent module name (or function name) in the log data?
for example, have log.info show something like "function blah() in setup.py INFO:" ?
Brigitta Sipocz
@bsipocz
Sep 01 2016 18:42
as I recall it's a subclass of the normal logger, so you can define the format parametrically and putting in the variables when needed? I can't try and play with it now though, so hopefully someone will come with a working example.
Megan Sosey
@sosey
Sep 01 2016 18:44
If it's not already there you should be able to attach a standard logging.Formatter that includes the funcName
Megan Sosey
@sosey
Sep 01 2016 20:23

out of meeting now, here’s an example, if I understand what you’re looking for? Or maybe it helps get you to it?

import logging
from astropy import logger
from logging import Formatter

mylog=logger.AstropyLogger('test')
myformat=Formatter(‘\nfuncttion:%(funcName)s \n%(message)s')
fh=logging.FileHandler(filename='test.log',mode='a')
fh.setLevel(logging.INFO)
fh.setFormatter(myformat)

mylog.addHandler(fh)
mylog.info('testing')

test.log now has:

<module>
testing

Adam Ginsburg
@keflavich
Sep 01 2016 20:55
@sosey: thanks. That's a lot of overhead for something apparently simple, but at least it illustrates the point.
Megan Sosey
@sosey
Sep 01 2016 20:56
yah - you might not need all of that, I was just trying to make a complete example. There must be a better way to change the formatter in the astropy logger if you just want to add information to the default log. I’d just have to look at the code and think about it.
Adam Ginsburg
@keflavich
Sep 01 2016 20:57
fair enough
still, that should work - if I want to use it in a package, I would write that into a support file; I'm hesitant to bring that in to individual scripts in its full bulk now
hmm
I guess that could also be used at import time to monkeypatch astropy.logger…
Megan Sosey
@sosey
Sep 01 2016 21:00
now I’m curious, lemme go see whats in astropy.logger...
Megan Sosey
@sosey
Sep 01 2016 22:40

hmm, I don’t see a convenient way to change the format once the logger has been created. You can grab the astropy logger and add new handlers or delete the ones there, or set the formatter statement in the astropy.cfg file in ~/.astropy/config/astropy.cfg to include the funcName.

If you want to do it live in code, the default logger has a StreamHandler, you could delete that (but you don’t have to) and replace it with your own if you want, still about the same code:

import logging
from astropy import log, logger
[log.removeHandler(f) for f in log.handlers if isinstance(f, logger.StreamHandler)]

fh=logger.StreamHandler()
myformat=logging.Formatter('\nfuncttion:%(funcName)s \n%(message)s')
fh.setFormatter(myformat)
log.addHandler(fh)

There’s probably someone here with better knowledge about the logging system than me that might have more suggestions…..

Adam Ginsburg
@keflavich
Sep 01 2016 23:26
sosey: I like that. It may be worth including as an example in the docs… perhaps someone would suggest a better alternative if you PR'd it, but otherwise it's workable