0

I am working Python and I need to logger so I decided to start using RotatingFileHandler. Below is my logging.conf file

[loggers]
keys=root

[handlers]
keys=logfile

[formatters]
keys=logfileformatter

[logger_root]
level=DEBUG
handlers=logfile

[formatter_logfileformatter]
format=%(asctime)s %(name)-12s: %(levelname)s %(message)s

[handler_logfile]
class=handlers.RotatingFileHandler
level=NOTSET
args=('ookagent.log', 'a', 50000000000, 5)
formatter=logfileformatter

And below is my Python script from which I am successfully able to log to the files. But I am not sure how to log both to files and console as well.

#!/usr/bin/python
import logging
import logging.config
import logging.handlers

# using RotatingFileHandler for logging purpose
logging.config.fileConfig('logging.conf')
ooklogger = logging.getLogger('')


ooklogger.info("HelloWorld")

Can we make a change in my logging.conf file by which I can login both to console and files as well? Is it possible to do that?

5
  • Why do you want to do that? Commented Dec 24, 2013 at 22:22
  • This is just for my debugging purpose.. In production I won't use this.. I am testing something and everytime I need to go and look for the files.. Commented Dec 24, 2013 at 22:23
  • Are you using linux or Mac? Commented Dec 24, 2013 at 22:25
  • I am running on Ubuntu 12.04. Forgot to mention.. Commented Dec 24, 2013 at 22:28
  • Well i think you can use the command tail with the argument -f this in one console this will watch the file and when a new line is added this one will appear in the console for example: tail -f myfile.log Commented Dec 24, 2013 at 22:30

1 Answer 1

1

Sure, the logging configuration file format lets you specify multiple handlers. You can use a StreamHandler to log to the console. That would entail modifications like these to your config file:

[handlers]
keys=logfile,logconsole

[handler_logconsole]
class=StreamHandler
# other configuration directives as you like

[logger_root]
handlers=logfile,logconsole

See the config file documentation for more information and examples.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for suggestion. I tried that and it gives me error as NoOptionError and I guess I need to add some configuration as you have mentioned in your answer but what configuration I should be adding for logging to console? I am little bit rusty in Python as it is not my first language.
That's entirely up to you. But as I said, see the documentation for more information and examples.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.