Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

logging - multiple log file

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')
Comment

logging - multiple log file

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')
Comment

PREVIOUS NEXT
Code Example
Python :: python import file from parent directory 
Python :: Get files from S3 bucket Python 
Python :: how to use pafy 
Python :: python timestamp to yyyy-mm-dd 
Python :: pandas return specific row 
Python :: tensorflow bert implementation 
Python :: Python program to draw star 
Python :: create django group 
Python :: python if in list multiple 
Python :: wait in python 
Python :: how to import from parent directory 
Python :: how to make an infinite loop python 
Python :: empty dictionary python 
Python :: for i 
Python :: python how to remove commas from string 
Python :: insert data in table python 
Python :: random library python 
Python :: pandas pass two columns to function 
Python :: python isnan 
Python :: what does .shape do in python 
Python :: pandas select rows by multiple conditions 
Python :: back button django template 
Python :: count characters in string python 
Python :: remove tuple from list python 
Python :: how to check for missing values in pandas 
Python :: 3d array numpy 
Python :: np where nan 
Python :: python lambda 
Python :: how to read multiple csv file from different directory in python 
Python :: Calculate Euclidean Distance in Python using distance.euclidean() 
ADD CONTENT
Topic
Content
Source link
Name
5+9 =