Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python logger

# Logger setup in python
def get_logger(self) -> logging.RootLogger:
    """instance of logger module, will be used for logging operations"""
    
    # logger config
    logger = logging.getLogger(__name__)
    logger.setLevel(logging.DEBUG)

    # log format
    lg_format = "%(levelname)s|%(filename)s:%(lineno)d|%(asctime)s|%(message)s"
    log_format = logging.Formatter(lg_format)

    # file handler
    file_handler = logging.FileHandler("log/monitor_model.log")
    file_handler.setFormatter(log_format)

    logger.handlers.clear()
    logger.addHandler(file_handler)
    return logger
Comment

logger format

formatter = logging.Formatter('[%(asctime)s] p%(process)s {%(pathname)s:%(lineno)d} %(levelname)s - %(message)s','%m-%d %H:%M:%S')
Comment

logger

	dependencies {
	        implementation 'com.github.Minecraftian14:MyLOGGER:v5.3'
	}
Comment

loggers

How do you use Log4J in your framework?
printing/logging the important events of the application/test run.
in my project I did logging using the log4j library.
I added the library dependency into pom.xml.
For logging we create an object from
Logger Interface and LogManager class using
getLogger method and passing the class name in it;  

private static Logger log = LogManager.getLogger(LogDemo.class.getName());
static Logger log = Logger.getLogger(log4jExample.class.getName());

We create it by passing the name of the current class.
Then we can use this object to do our logging.
log.info
log.debug
log.fatal
log.error

The Logger object is responsible for capturing 
logging information and they are stored in a namespace hierarchy.
Comment

logger

[loggers]
keys=root

[handlers]
keys=console, file

[formatters]
keys=std_out

[logger_root]
handlers = console, file
level = DEBUG

[handler_console]
class = logging.StreamHandler
level = DEBUG
formatter = std_out


[handler_file]
class = logging.FileHandler
kwargs = {"filename": "all_messages_conf.log"}
level = INFO
formatter = std_out

[formatter_std_out]
format = %(levelname)s : %(name)s : %(module)s : %(funcName)s : %(message)s
Comment

logger

import logging

def get_logger(log_file_name, log_sub_dir=""):
    """ Creates a Log File and returns Logger object """
    
    # Build Log File Full Path
    logPath = log_file_name if os.path.exists(log_file_name) else os.path.join(log_dir, (str(log_file_name) + '.log'))

    # Create logger object and set the format for logging and other attributes
    logger = logging.Logger(log_file_name)

    # Return logger object
    return logger
Comment

PREVIOUS NEXT
Code Example
Python :: how to add pagination in discord.py 
Python :: python environment 
Python :: command for python shell 
Python :: pandas get row if difference previous 
Python :: timedelta format python 
Python :: Label enconding code with sklearn 
Python :: how to split a string by colon in python 
Python :: sum of even numbers 
Python :: import sentence transformers 
Python :: Python DateTime Date Class Syntax 
Python :: title() in python 
Python :: pandas weighted average groupby 
Python :: print() function in python 
Python :: pivot table pandas 
Python :: python string: .strip() 
Python :: heroku how to access config vars django 
Python :: phyton "2.7" print 
Python :: how to add element to list python 
Python :: Reverse an string Using Reversed function 
Python :: percentage plot of categorical variable in python woth hue 
Python :: python environment variable 
Python :: How to solve not in base 10 in python when using decimals 
Python :: django model 
Python :: split rows into multiple columns in pandas 
Python :: python singleton module 
Python :: np where and 
Python :: python pytest vs unittest 
Python :: python error 
Python :: pandas drop rows 
Python :: what is the ternary operator in python 
ADD CONTENT
Topic
Content
Source link
Name
4+9 =