11
submitted 3 months ago* (last edited 3 months ago) by [email protected] to c/[email protected]

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.

you are viewing a single comment's thread
view the rest of the comments
[-] [email protected] 3 points 3 months ago* (last edited 3 months ago)

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.

[-] [email protected] 1 points 3 months ago* (last edited 3 months ago)

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)

this post was submitted on 25 Mar 2024
11 points (100.0% liked)

Python

5949 readers
3 users here now

Welcome to the Python community on the programming.dev Lemmy instance!

📅 Events

October 2023

November 2023

PastJuly 2023

August 2023

September 2023

🐍 Python project:
💓 Python Community:
✨ Python Ecosystem:
🌌 Fediverse
Communities
Projects
Feeds

founded 1 year ago
MODERATORS