how to send the icecream output to a log file? [solved]
how to send the icecream output to a log file? [solved]
Hi, I'm currently using this to log my python process
logging.basicConfig(filename='filename.log', level=logging.DEBUG)
logger = logging.getLogger()
sys.stderr.write = logger.error
sys.stdout.write = logger.info
And then using print(f'{datetime.now()} log message') where I want to log.
It's working OK, buy I would like to live it to ic, but can't find any info on how to send the ic output to the logger.
Thanks for any help.
Overwriting
sys.stdout
andsys.stderr
seems like a Bad Idea™.Browsing the icecream source code, it looks like you can do this:
logger = logging.getLogger() ic.configureOutput(outputFunction=logger.debug)
And then just make sure the logger is configured to actually print at DEBUG level.
4 0 ReplyThanks, tried the next
logging.basicConfig(filename='filename.log', level=logging.DEBUG) logger = logging.getLogger() ic.configureOutput(outputFunction=logger.debug) ic('test')
But didn't create the log file and the output was printed on the screen and not on log file(that was never created anyway)
1 0 ReplyJust to confirm, if you change
ic('test')
tologger.debug('test')
it works as expected? I.e. it creates the log file?Edit: Also if you change the output format of the logger and use it as the
outputFunction
toic
, does it include the customized format?I just want to confirm if your
ic
is routing through the logger or not, to know if it's a problem with your logging config or withic
.3 0 Reply
Never used
ic
before, but there is an example in the README on GitHub.>>> import logging >>> from icecream import ic >>> >>> def warn(s): >>> logging.warning(s) >>> >>> ic.configureOutput(outputFunction=warn) >>> ic('eep') WARNING:root:ic| 'eep': 'eep'
2 0 ReplyThanks, tried the next:
logging.basicConfig(filename='filename.log', level=logging.DEBUG) logger = logging.getLogger() def warn(s): logging.warn(s) ic.configureOutput(outputFunction=warn)
But it didn't create the .log file and the ic output was printed on the screen instead of the file (that was never created anyway)
1 0 Reply
If you're talking about the package icecream, there's "ic.format()" that returns a string you can log, instead of printing directly to standard out.
More generically, you can have pythons logger capture all of standard out, but that's kind of a shotgun approach, and depending on what your doing, may have some side effects you don't want.
1 0 Reply