Answers for "how to read log files"

6

making log files in python

import logging
logging.basicConfig(filename='example.log', encoding='utf-8', level=logging.DEBUG)
logging.debug('This is debug message')
logging.info('This is information message')
logging.warning('This is warning message')
logging.error('This is warning message')
Posted by: Guest on July-12-2021
6

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")
Posted by: Guest on August-13-2020
1

logging to a file log4j

// How to log to a file using log4j : 

 String filePath = "D:/Logs/MyFirstLog.log";
 PatternLayout layout = new PatternLayout("%-5p %d %m%n");
 RollingFileAppender appender = new RollingFileAppender(layout, filePath);
 appender.setName("MyFirstLog");
 appender.setMaxFileSize("20MB");
 appender.activateOptions();
 logger.addAppender(appender);
 logger.info("this will be printed in the provided log file");
Posted by: Guest on September-06-2021

Python Answers by Framework

Browse Popular Code Answers by Language