Question : create log in python
Answered by : deepakchakravarthy
logging.basicConfig(filename="logfilename.log", level=logging.INFO)
# Log Creation
logging.info('your text goes here')
logging.error('your text goes here')
logging.debug('your text goes here')
Source : https://stackoverflow.com/questions/49580313/create-a-log-file?rq=1 | Last Update : Wed, 04 Nov 20
Question : python logger to different file
Answered by : super-snail-yc02tqprrjhw
import logging
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
def setup_logger(name, log_file, level=logging.INFO): """To setup as many loggers as you want""" handler = logging.FileHandler(log_file) handler.setFormatter(formatter) logger = logging.getLogger(name) logger.setLevel(level) logger.addHandler(handler) return logger
# first file logger
logger = setup_logger('first_logger', 'first_logfile.log')
logger.info('This is just info message')
# second file logger
super_logger = setup_logger('second_logger', 'second_logfile.log')
super_logger.error('This is an error message')
def another_method(): # using logger defined above also works here logger.info('Inside method')
Source : https://stackoverflow.com/questions/11232230/logging-to-two-files-with-different-settings | Last Update : Mon, 07 Mar 22
Question : python log file
Answered by : tense-termite-fknd4u7gmanv
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ö')
Source : https://docs.python.org/es/3/howto/logging.html | Last Update : Mon, 21 Jun 21
Question : python logger format
Answered by : magnificent-mongoose-9ihc5o47fa63
FORMAT = '%(asctime)s %(clientip)-15s %(user)-8s %(message)s'
logging.basicConfig(format=FORMAT)
d = {'clientip': '192.168.0.1', 'user': 'fbloggs'}
logger = logging.getLogger('tcpserver')
logger.warning('Protocol problem: %s', 'connection reset', extra=d)
Source : https://docs.python.org/3/library/logging.html | Last Update : Thu, 05 May 22
Question : tail a log file with python
Answered by : guissou-abdoul-fayal
pip or pip3 install sh
from sh import tail
# runs forever
for line in tail("-f", "/var/log/some_log_file.log", _iter=True): print(line)
Source : https://newbedev.com/how-can-i-tail-a-log-file-in-python | Last Update : Mon, 15 Nov 21