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.

cbarrick,

Overwriting sys.stdout and sys.stderr seems like a Bad Idea™.

Browsing the icecream source code, it looks like you can do this:


<span style="color:#323232;">logger = logging.getLogger()
</span><span style="color:#323232;">ic.configureOutput(outputFunction=logger.debug)
</span>

And then just make sure the logger is configured to actually print at DEBUG level.

driving_crooner,
@driving_crooner@lemmy.eco.br avatar

Thanks, tried the next


<span style="color:#323232;">logging.basicConfig(filename='filename.log', level=logging.DEBUG)
</span><span style="color:#323232;">
</span><span style="color:#323232;">logger = logging.getLogger()
</span><span style="color:#323232;">ic.configureOutput(outputFunction=logger.debug)
</span><span style="color:#323232;">
</span><span style="color:#323232;">ic('test')
</span><span style="color:#323232;">
</span>

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)

cbarrick,

Just to confirm, if you change ic(‘test’) to logger.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 to ic, 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 with ic.

driving_crooner, (edited )
@driving_crooner@lemmy.eco.br avatar

EDIT: I think I had something wrong on the logging.basicConfig() because now is creating the .log file when running that line and then the ic() is writing correctly on the log file.

Edit 2: I found what was happening, I was using another directory and now found the og log file with all the tests that were supposed lost.

Thanks for your help.

logger.debug(‘test’) created the log file and logged test, after that ic(‘test’) wrote correctly on the log file.

Restating the kernel and running again directly with ic(‘test’) it wrote on the already existing log file.

So, to recap. If the .log file doesn’t exists, ic() didn’t create the file and nothing is logged, if the the log file already exists, then the ic() wrote correctly on the log file.

The only missing part is how to create the log file without needing of the logger.debub(‘’).

Tried to create an empty log file with open(‘logfile.log’, ‘a’).close() and the log file is created but is useless (dosen’t even write the logger.debug(‘test’) output)

nxdefiant,

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.

sloppy_diffuser,

Never used ic before, but there is an example in the README on GitHub.


<span style="color:#323232;">>>> import logging
</span><span style="color:#323232;">>>> from icecream import ic
</span><span style="color:#323232;">>>>
</span><span style="color:#323232;">>>> def warn(s):
</span><span style="color:#323232;">>>>     logging.warning(s)
</span><span style="color:#323232;">>>>
</span><span style="color:#323232;">>>> ic.configureOutput(outputFunction=warn)
</span><span style="color:#323232;">>>> ic('eep')
</span><span style="color:#323232;">WARNING:root:ic| 'eep': 'eep'
</span>
driving_crooner,
@driving_crooner@lemmy.eco.br avatar

Thanks, tried the next:


<span style="color:#323232;">logging.basicConfig(filename='filename.log', level=logging.DEBUG)
</span><span style="color:#323232;">
</span><span style="color:#323232;">logger = logging.getLogger()
</span><span style="color:#323232;">
</span><span style="color:#323232;">def warn(s):
</span><span style="color:#323232;">    logging.warn(s)
</span><span style="color:#323232;">
</span><span style="color:#323232;">ic.configureOutput(outputFunction=warn)
</span>

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)

  • All
  • Subscribed
  • Moderated
  • Favorites
  • python@programming.dev
  • DreamBathrooms
  • magazineikmin
  • tacticalgear
  • cisconetworking
  • ethstaker
  • rosin
  • love
  • Youngstown
  • slotface
  • khanakhh
  • kavyap
  • osvaldo12
  • thenastyranch
  • everett
  • provamag3
  • InstantRegret
  • tester
  • normalnudes
  • ngwrru68w68
  • cubers
  • GTA5RPClips
  • mdbf
  • Durango
  • anitta
  • modclub
  • Leos
  • megavids
  • JUstTest
  • All magazines