Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

logging store info to different files

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 function create null matrix 
Python :: what is the size of cover in facebook 
Python :: python MAX_INT 
Python :: Convert a Pandas Column of Timestamps to Datetimes 
Python :: simple python game code 
Python :: topological sort 
Python :: clear many to many django 
Python :: django prevent duplicate entries 
Python :: python string contains substring ignore case 
Python :: handling timezone in python 
Python :: plot dataframe 
Python :: import from parent directory python 
Python :: Subtract different times in Python 
Python :: change value in tuple 
Python :: python catch print 
Python :: add values from 2 columns to one pandas 
Python :: How to check the number of occurence of each character of a given string in python 
Python :: discord bot python get message id 
Python :: pytest fixture 
Python :: replace nan in pandas column with mode and printing it 
Python :: discord py join and leave call 
Python :: convert a list to tuple 
Python :: django action when create model 
Python :: numpy linspace function 
Python :: lambda functions python 
Python :: python turtle tutorial 
Python :: convert date to string in python 
Python :: run python file from cmd 
Python :: python shift number 
Python :: python describe 
ADD CONTENT
Topic
Content
Source link
Name
7+5 =