Exemplo n.º 1
0
    def _init_consolelogger(self):
        if self.config.get('log', 'color_console') and ismodule('colorlog'):
            fmt = "%(label)s %(levelname)-8s %(reset)s %(log_color)s%(asctime)s  %(message)s"
            datefmt = "%Y-%m-%d  %H:%M:%S"
            primary_colors = {
                'DEBUG': "bold,cyan",
                'WARNING': "bold,yellow",
                'ERROR': "bold,red",
                'CRITICAL': "bold,purple",
            }
            secondary_colors = {
                'label': {
                    'DEBUG': "bold,white,bg_cyan",
                    'INFO': "bold,white,bg_green",
                    'WARNING': "bold,white,bg_yellow",
                    'ERROR': "bold,white,bg_red",
                    'CRITICAL': "bold,white,bg_purple",
                }
            }
            consoleform = colorlog.ColoredFormatter(
                fmt,
                datefmt,
                primary_colors,
                secondary_log_colors=secondary_colors)
        else:
            fmt = "%(asctime)s  %(levelname)-8s  %(message)s"
            datefmt = "%Y-%m-%d %H:%M:%S"
            consoleform = logging.Formatter(fmt, datefmt)

        consolehdlr = logging.StreamHandler(sys.stdout)
        consolehdlr.setFormatter(consoleform)
        self.log.addHandler(consolehdlr)
Exemplo n.º 2
0
    def _setup_console(logger, config):
        if config.get(
                'log', 'colorlog') and ismodule('colorlog'):
            fmt = '%(label)s %(levelname)-8s %(reset)s %(log_color)s%(asctime)s  %(message)s'
            datefmt = '%Y-%m-%d  %H:%M:%S'
            primary_colors = {
                'DEBUG': 'bold,cyan',
                'WARNING': 'bold,yellow',
                'ERROR': 'bold,red',
                'CRITICAL': 'bold,purple',
            }
            secondary_colors = {
                'label': {
                    'DEBUG': 'bold,white,bg_cyan',
                    'INFO': 'bold,white,bg_green',
                    'WARNING': 'bold,white,bg_yellow',
                    'ERROR': 'bold,white,bg_red',
                    'CRITICAL': 'bold,white,bg_purple',
                }
            }
            consoleform = colorlog.ColoredFormatter(
                fmt, datefmt, primary_colors,
                secondary_log_colors=secondary_colors)
        else:
            fmt = '%(asctime)s  %(levelname)-8s  %(message)s'
            datefmt = '%Y-%m-%d %H:%M:%S'
            consoleform = logging.Formatter(fmt, datefmt)

        consolehdlr = logging.StreamHandler(sys.stdout)
        consolehdlr.setFormatter(consoleform)
        logger.addHandler(consolehdlr)
Exemplo n.º 3
0
    def _setup_console(logger, config):
        if config.get('log', 'colorlog') and ismodule('colorlog'):
            fmt = '%(label)s %(levelname)-8s %(reset)s %(log_color)s%(asctime)s  %(message)s'
            datefmt = '%Y-%m-%d  %H:%M:%S'
            primary_colors = {
                'DEBUG': 'bold,cyan',
                'WARNING': 'bold,yellow',
                'ERROR': 'bold,red',
                'CRITICAL': 'bold,purple',
            }
            secondary_colors = {
                'label': {
                    'DEBUG': 'bold,white,bg_cyan',
                    'INFO': 'bold,white,bg_green',
                    'WARNING': 'bold,white,bg_yellow',
                    'ERROR': 'bold,white,bg_red',
                    'CRITICAL': 'bold,white,bg_purple',
                }
            }
            consoleform = colorlog.ColoredFormatter(
                fmt,
                datefmt,
                primary_colors,
                secondary_log_colors=secondary_colors)
        else:
            fmt = '%(asctime)s  %(levelname)-8s  %(message)s'
            datefmt = '%Y-%m-%d %H:%M:%S'
            consoleform = logging.Formatter(fmt, datefmt)

        consolehdlr = logging.StreamHandler(sys.stdout)
        consolehdlr.setFormatter(consoleform)
        logger.addHandler(consolehdlr)
Exemplo n.º 4
0
    def _init_logger(self, level):
        # Init logger
        self.log = logging.getLogger('pyload')
        self.log.setLevel(level)

        # Set console handler
        if self.config.get('log', 'color_console') and ismodule('colorlog'):
            fmt = "%(label)s %(levelname)-8s %(reset)s %(log_color)s%(asctime)s  %(message)s"
            datefmt = "%Y-%m-%d  %H:%M:%S"
            log_colors = {
                'DEBUG': "bold,cyan",
                'WARNING': "bold,yellow",
                'ERROR': "bold,red",
                'CRITICAL': "bold,purple",
            }
            log_colors_2 = {
                'label': {
                    'DEBUG': "bold,white,bg_cyan",
                    'INFO': "bold,white,bg_green",
                    'WARNING': "bold,white,bg_yellow",
                    'ERROR': "bold,white,bg_red",
                    'CRITICAL': "bold,white,bg_purple",
                }
            }
            consoleform = colorlog.ColoredFormatter(
                fmt, datefmt, log_colors, secondary_log_colors=log_colors_2)
        else:
            fmt = "%(asctime)s  %(levelname)-8s  %(message)s"
            datefmt = "%Y-%m-%d %H:%M:%S"
            consoleform = logging.Formatter(fmt, datefmt)

        consolehdlr = logging.StreamHandler(sys.stdout)
        consolehdlr.setFormatter(consoleform)
        self.log.addHandler(consolehdlr)

        # Set syslog handler
        syslog = self.config.get('log', 'syslog')
        if syslog != 'no':
            #: try to mimic to normal syslog messages
            fmt = "%(asctime)s %(name)s: %(message)s"
            datefmt = "%b %e %H:%M:%S"
            syslogform = logging.Formatter(fmt, datefmt)
            syslogaddr = None

            if syslog == 'remote':
                syslog_host = self.config.get('log', 'syslog_host')
                syslog_port = self.config.get('log', 'syslog_port')
                syslogaddr = (syslog_host, syslog_port)
            else:
                syslog_folder = self.config.get('log', 'syslog_folder')
                if syslogaddr:
                    syslogaddr = syslog_folder
                elif sys.platform == 'darwin':
                    syslogaddr = '/var/run/syslog'
                elif os.name != 'nt':
                    syslogaddr = '/dev/log'

            sysloghdlr = logging.handlers.SysLogHandler(syslogaddr)
            sysloghdlr.setFormatter(syslogform)
            self.log.addHandler(sysloghdlr)

        # Create logfile folder
        logfile_folder = self.config.get('log', 'logfile_folder')
        if not logfile_folder:
            logfile_folder = os.path.abspath("logs")
        makedirs(logfile_folder)

        # Set file handler
        if not self.config.get('log', 'logfile'):
            return None

        fmt = "%(asctime)s  %(levelname)-8s  %(message)s"
        datefmt = "%Y-%m-%d %H:%M:%S"
        fileform = logging.Formatter(fmt, datefmt)

        logfile = os.path.join(logfile_folder, 'log.txt')
        if self.config.get('log', 'rotate'):
            logfile_size = self.config.get('log', 'logfile_size') << 10
            max_logfiles = self.config.get('log', 'max_logfiles')
            filehdlr = logging.handlers.RotatingFileHandler(
                logfile,
                maxBytes=logfile_size,
                backupCount=max_logfiles,
                encoding=getpreferredencoding())
        else:
            filehdlr = logging.FileHandler(logfile,
                                           encoding=getpreferredencoding())

        filehdlr.setFormatter(fileform)
        self.log.addHandler(filehdlr)
Exemplo n.º 5
0
    def _init_logger(self, level):
        # Init logger
        self.log = logging.getLogger('pyload')
        self.log.setLevel(level)

        # Set console handler
        if self.config.get('log', 'color_console') and ismodule('colorlog'):
            fmt = "%(label)s %(levelname)-8s %(reset)s %(log_color)s%(asctime)s  %(message)s"
            datefmt = "%Y-%m-%d  %H:%M:%S"
            log_colors = {
                'DEBUG': "bold,cyan",
                'WARNING': "bold,yellow",
                'ERROR': "bold,red",
                'CRITICAL': "bold,purple",
            }
            log_colors_2 = {
                'label': {
                    'DEBUG': "bold,white,bg_cyan",
                    'INFO': "bold,white,bg_green",
                    'WARNING': "bold,white,bg_yellow",
                    'ERROR': "bold,white,bg_red",
                    'CRITICAL': "bold,white,bg_purple",
                }
            }
            consoleform = colorlog.ColoredFormatter(fmt, datefmt, log_colors,
                                                    secondary_log_colors=log_colors_2)
        else:
            fmt = "%(asctime)s  %(levelname)-8s  %(message)s"
            datefmt = "%Y-%m-%d %H:%M:%S"
            consoleform = logging.Formatter(fmt, datefmt)

        consolehdlr = logging.StreamHandler(sys.stdout)
        consolehdlr.setFormatter(consoleform)
        self.log.addHandler(consolehdlr)

        # Set syslog handler
        syslog = self.config.get('log', 'syslog')
        if syslog != 'no':
            #: try to mimic to normal syslog messages
            fmt = "%(asctime)s %(name)s: %(message)s"
            datefmt = "%b %e %H:%M:%S"
            syslogform = logging.Formatter(fmt, datefmt)
            syslogaddr = None

            if syslog == 'remote':
                syslog_host = self.config.get('log', 'syslog_host')
                syslog_port = self.config.get('log', 'syslog_port')
                syslogaddr = (syslog_host, syslog_port)
            else:
                syslog_folder = self.config.get('log', 'syslog_folder')
                if syslogaddr:
                    syslogaddr = syslog_folder
                elif sys.platform == 'darwin':
                    syslogaddr = '/var/run/syslog'
                elif os.name != 'nt':
                    syslogaddr = '/dev/log'

            sysloghdlr = logging.handlers.SysLogHandler(syslogaddr)
            sysloghdlr.setFormatter(syslogform)
            self.log.addHandler(sysloghdlr)

        # Create logfile folder
        logfile_folder = self.config.get('log', 'logfile_folder')
        if not logfile_folder:
            logfile_folder = os.path.abspath("logs")
        makedirs(logfile_folder)

        # Set file handler
        if not self.config.get('log', 'logfile'):
            return None

        fmt = "%(asctime)s  %(levelname)-8s  %(message)s"
        datefmt = "%Y-%m-%d %H:%M:%S"
        fileform = logging.Formatter(fmt, datefmt)

        logfile = os.path.join(logfile_folder, 'log.txt')
        if self.config.get('log', 'rotate'):
            logfile_size = self.config.get('log', 'logfile_size') << 10
            max_logfiles = self.config.get('log', 'max_logfiles')
            filehdlr = logging.handlers.RotatingFileHandler(logfile,
                                                            maxBytes=logfile_size,
                                                            backupCount=max_logfiles,
                                                            encoding=getpreferredencoding())
        else:
            filehdlr = logging.FileHandler(
                logfile, encoding=getpreferredencoding())

        filehdlr.setFormatter(fileform)
        self.log.addHandler(filehdlr)