log file.

Joaquin Alzola Joaquin.Alzola at lebara.com
Tue Jun 14 16:24:21 EDT 2016


>thanks I will look at them.

As an example for your guide:

##log_handler###
import sys
import logging
import logging.handlers
from configfile_read import *

def LogHandler(logger):
        CONFIG_FILE = ('./conf/' + 'config.ini')
        config = configfile(CONFIG_FILE)

        FORMAT = '%(asctime)-15s %(name)s %(levelname)s-%(levelno)s %(message)s'
        LOG_LEVEL = config.get("logging","Logging_Level")
        LOG_FILE = config.get("logging","Logging_File")
        LOG_DIRECTORY = config.get("logging","Logging_Directory")
        LOG_BYTES = config.get("logging","Logging_bytes")
        LOG_COUNT = config.get("logging","Logging_count")

        LEVEL = logging.getLevelName(LOG_LEVEL)
        logger.setLevel(LEVEL)
        handler = logging.handlers.RotatingFileHandler(LOG_DIRECTORY + LOG_FILE,maxBytes=int(LOG_BYTES),backupCount=int(LOG_COUNT))
        handler.setFormatter(logging.Formatter(FORMAT))
        return handler


On each part of your code start a logger:
logger = logging.getLogger('__main__.' + __name__)

And in the main part:
from log_handler import *
#Logging Config Starts
logger = logging.getLogger(__name__)
logger.addHandler(LogHandler(logger))
#Logging Config Ends

####Config_parser where log parameters are taken####
import configparser
import os, sys
import logging
import logging.handlers
from log_handler import *

logger = logging.getLogger('__main__.' + __name__)

class configfile:

        def __init__(self,CONFIG_FILE):
                self.CONFIG_FILE = CONFIG_FILE
                if not os.path.isfile(self.CONFIG_FILE):
                        logger.critical('File NOT found: %s' % CONFIG_FILE + '. This file is needed for the system to work')
                        sys.exit()

        def get(self,general,parameter):
                config = configparser.ConfigParser()
                config.read(self.CONFIG_FILE)
                try:
                        result = config.get(general,parameter)
                except configparser.NoSectionError as e:
                        logger.critical('File %s is there but need information inside: %s'%(self.CONFIG_FILE,e))
                        sys.exit()
                logger.debug('Parameters pass to configparser-->' + general + ':' + parameter + ' with result-->' + result)
                return result


testbedocg:/usr/local/statistics_server/conf # more config.ini
[config]
Host=172.1.1.1
Port=12346

[logging]
#Possbile Values: CRITICAL ERROR WARNING INFO DEBUG NOTSET
Logging_Level=DEBUG
Logging_File=server_log.log
Logging_Directory=./logs/
Logging_bytes=2097152
Logging_count=5

This email is confidential and may be subject to privilege. If you are not the intended recipient, please do not copy or disclose its content but contact the sender immediately upon receipt.



More information about the Python-list mailing list