Omar
0
Q:

python logging to file

import logging

"""
DEBUG
INFO
WARNING
ERROR
CRITICAL
"""
# asctime: time of the log was printed out
# levelname: name of the log
# datefmt: format the time of the log
# give DEBUG log
logging.basicConfig(format='%(asctime)s %(levelname)-8s [%(filename)s:%(lineno)d] %(message)s',
    datefmt='%d-%m-%Y:%H:%M:%S',
    level=logging.DEBUG,
    filename='logs.txt')

logger = logging.getLogger('my_app')

logger.debug("This is a debug log")
logger.info("This is an info log")
logger.critical("This is critical")
logger.error("An error occurred")
1
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')
0
import logging

logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s [%(levelname)s] %(message)s",
    handlers=[
        logging.FileHandler("debug.log"),
        logging.StreamHandler()
    ]
)
1
import logging

rfh = logging.handlers.RotatingFileHandler(
 	filename='foo.log', 
  	mode='a',
  	maxBytes=20*1024*1024,
  	backupCount=2,
  	encoding=None,
  	delay=0
)

logging.basicConfig(
  	level=logging.DEBUG,
  	format="%(asctime)s %(name)-25s %(levelname)-8s %(message)s",
    datefmt="%y-%m-%d %H:%M:%S",
    handlers=[
    	rfh
    ]
)

logger = logging.getLogger('main')

logger.debug("test")
1

New to Communities?

Join the community