How to log input and output into a text file in Python? - python

Is there a way to save the program input and output to a text file? Let's have a really simple application and say I want to log the shell to a "log.txt" file.
What's your name?
Peter
How old are you?
35
Where are your from?
Germany
Hi Peter, you're 35 years old and you're from Germany!
I believe that the logging module could be the way but I don't know how to use it.

In the official documentation there is a Basic Logging Tutorial.
A very simple example would be:
import logging
logging.basicConfig(filename='example.log', encoding='utf-8', level=logging.DEBUG)
logging.debug('This message should go to the log file')
logging.info('So should this')
logging.warning('And this, too')
logging.error('And non-ASCII stuff, too, like Øresund and Malmö')
That's the explanation:
First you import logging, and after that you configure it with basicConfig indicating the file that you want to save the log messages to.
Then, every time you want to add something to the log, you have to use the logging functions: debug(), info(), warning(), error() and critical(), that are named after the level or severity of the events they are used to track.

Related

Loguru - how to clear the logs

is there a way clear the log file before using it with the python log library 'loguru'?
Thanks!
from loguru import logger
# clear the log before logging
logger.add('output.log', format="{time:YYYY-MM-DD HH:mm:ss} {level} {message}")
logger.debug('my message')
Loguru API does not have the ability to remove/clean up log files. Instead of that, you could either use open('output.log', 'w').close() if you want to erase all contents of the log file. Or check the ability to use log rotation in Loguru if it's suitable for your use case (something like that logger.add("file_{time}.log"))

Should `logging.basicConfig(level=logging.INFO)` accept only INFO level of logging?

Here is the sample code from source: https://docs.python.org/3/howto/logging.html
import logging
logging.basicConfig(filename='example.log', encoding='utf-8', level=logging.DEBUG)
logging.debug('This message should go to the log file')
logging.info('So should this')
logging.warning('And this, too')
logging.error('And non-ASCII stuff, too, like Øresund and Malmö')
I thought the level=logging.DEBUG of below code
logging.basicConfig(filename='example.log', encoding='utf-8', level=logging.DEBUG)
making the logging accept only logging.DEBUG only. Then why info, warning, error work
The logging levels of python is arranged in the following order:
CRITICAL
ERROR
WARNING
INFO
DEBUG
NOTSET
If you set the root logger level when configuring as logging.DEBUG, it will write all the logs with the levels above that.
Example:
import logging
logging.basicConfig(filename='example.log', encoding='utf-8', level=logging.DEBUG)
logging.debug('This message should go to the log file')
logging.info('So should this')
logging.warning('And this, too')
logging.error('And non-ASCII stuff, too, like Øresund and Malmö')
Output:
DEBUG:root:This message should go to the log file
INFO:root:So should this
WARNING:root:And this, too
ERROR:root:And non-ASCII stuff, too, like Øresund and Malmö
If you set the root logger level when configuring as logging.ERROR, it will write only the CRITICAL logs and ERROR logs.
Example:
import logging
logging.basicConfig(filename='example.log', level=logging.ERROR)
logging.warning('this is warning')
logging.info('this is info')
logging.error('And non-ASCII stuff, too, like Øresund and Malmö')
logging.critical('And non-ASCII stuff, too, like Øresund and Malmö 2')
Output:
ERROR:root:And non-ASCII stuff, too, like Øresund and Malmö
CRITICAL:root:And non-ASCII stuff, too, like Øresund and Malmö 2
Log levels are actually integers and they set a threshold. For instance logging.DEBUG is 10 and logging.INFO is 20. When you set a log level, you allow that level and anything larger. When you set DEBUG any level 10 or greater will log. Filter objects are commonly used if you want a different level of control.

Save each level of logging in a different file

I am working with the Python logging module but I don't know how get writing in different files for each type of logging level.
For example, I want the debug-level messages (and only debug) saved in the file debug.log.
This is the code I have so far, I have tried with handlers but does not work anymore. I don't know If it is possible to do so I wish. This is the code I have using handlers and different files for each level of logging, but it does not work, since the Debug file save messages of all other levels.
import logging as _logging
self.logger = _logging.getLogger(__name__)
_handler_debug = _logging.FileHandler(self.debug_log_file)
_handler_debug.setLevel(_logging.DEBUG)
self.logger.addHandler(_handler_debug)
_handler_info = _logging.FileHandler(self.info_log_file)
_handler_info.setLevel(_logging.INFO)
self.logger.addHandler(_handler_info)
_handler_warning = _logging.FileHandler(self.warning_log_file)
_handler_warning.setLevel(_logging.WARNING)
self.logger.addHandler(_handler_warning)
_handler_error = _logging.FileHandler(self.error_log_file)
_handler_error.setLevel(_logging.ERROR)
self.logger.addHandler(_handler_error)
_handler_critical = _logging.FileHandler(self.critical_log_file)
_handler_critical.setLevel(_logging.CRITICAL)
self.logger.addHandler(_handler_critical)
self.logger.debug("debug test")
self.logger.info("info test")
self.logger.warning("warning test")
self.logger.error("error test")
self.logger.critical("critical test")
And the debug.log contains this:
debug test
info test
warning test
error test
critical test
I'm working with classes, so I have adapted a bit the code
Possible duplicate to: python logging specific level only
Please see the accepted answer there.
You need to use filters for each handler that restrict the output to the log level supplied.

Python Logging to head of logfile?

I use python logging and would like the latest log entry to be at the head of the log file, rather than the tail.
I cannot find anything in https://docs.python.org/2/howto/logging.html that appears useful for appending a logger record at the head of the logfile instead of the tail.
This is my logger: ("Carl" is my GoPiGo3 robot)
import logging
# create logger
logger = logging.getLogger('lifelog')
logger.setLevel(logging.INFO)
loghandler = logging.FileHandler('/home/pi/Carl/life.log')
logformatter = logging.Formatter('%(asctime)s|%(message)s',"%Y-%m-%d %H:%M")
loghandler.setFormatter(logformatter)
logger.addHandler(loghandler)
#logger.info('-------------')
and I log with:
logger.info('<something to log>')
Is there a python logging module intrinsic solution?
Do I have to write my own handler?
The Answer: I should not want to do this.
Because:
Inserting something at the beginning of a file displaces everything
that comes after it, so the bigger the file, the more an insert will
cost. That would go counter to the efforts of the logging module to be
sparing in its use of resources. – BoarGules Mar 28 at 13:56
Using 'tac' to view the log file is a good solution.

writing a log file from python program

I want to output some strings to a log file and I want the log file to be continuously updated.
I have looked into the logging module pf python and found out that it is
mostly about formatting and concurrent access.
Please let me know if I am missing something or amy other way of doing it
usually i do the following:
# logging
LOG = "/tmp/ccd.log"
logging.basicConfig(filename=LOG, filemode="w", level=logging.DEBUG)
# console handler
console = logging.StreamHandler()
console.setLevel(logging.ERROR)
logging.getLogger("").addHandler(console)
The logging part initialises logging's basic configurations. In the following I set up a console handler that prints out some logging information separately. Usually my console output is set to output only errors (logging.ERROR) and the detailed output in the LOG file.
Your loggings will now printed to file. For instance using:
logger = logging.getLogger(__name__)
logger.debug("hiho debug message")
or even
logging.debug("next line")
should work.
Doug Hellmann has a nice guide.
To add my 10cents with regards to using logging. I've only recently discovered the Logging module and was put off at first. Maybe just because it initially looks like a lot of work, but it's really simple and incredibly handy.
This is the set up that I use. Similar to Mkinds answer, but includes a timestamp.
# Set up logging
log = "bot.log"
logging.basicConfig(filename=log,level=logging.DEBUG,format='%(asctime)s %(message)s', datefmt='%d/%m/%Y %H:%M:%S')
logging.info('Log Entry Here.')
Which will produce something like:
22/09/2015 14:39:34 Log Entry Here.
You can log to a file with the Logging API.
Example: http://docs.python.org/2/howto/logging.html#logging-to-a-file

Categories