Python logging 模块,FileHandler() 实例源码

我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用logging.FileHandler()

项目:safetyculture-sdk-python    作者:SafetyCulture    | 项目源码 | 文件源码
def configure_logging(path_to_log_directory):
    """
    Configure logger

    :param path_to_log_directory:  path to directory to write log file in
    :return:
    """
    log_filename = datetime.datetime.now().strftime('%Y-%m-%d') + '.log'
    importer_logger = logging.getLogger('importer_logger')
    importer_logger.setLevel(LOG_LEVEL)
    formatter = logging.Formatter('%(asctime)s : %(levelname)s : %(message)s')

    fh = logging.FileHandler(filename=os.path.join(path_to_log_directory, log_filename))
    fh.setLevel(LOG_LEVEL)
    fh.setFormatter(formatter)
    importer_logger.addHandler(fh)

    sh = logging.StreamHandler(sys.stdout)
    sh.setLevel(LOG_LEVEL)
    sh.setFormatter(formatter)
    importer_logger.addHandler(sh)
项目:test-infra    作者:istio    | 项目源码 | 文件源码
def setup_logging(path):
    """Initialize logging to screen and path."""
    # See https://docs.python.org/2/library/logging.html#logrecord-attributes
    # [IWEF]mmdd HH:MM:SS.mmm] msg
    fmt = '%(levelname).1s%(asctime)s.%(msecs)03d] %(message)s'  # pylint: disable=line-too-long
    datefmt = '%m%d %H:%M:%S'
    logging.basicConfig(
        level=logging.INFO,
        format=fmt,
        datefmt=datefmt,
    )
    build_log = logging.FileHandler(filename=path, mode='w')
    build_log.setLevel(logging.INFO)
    formatter = logging.Formatter(fmt, datefmt=datefmt)
    build_log.setFormatter(formatter)
    logging.getLogger('').addHandler(build_log)
    return build_log
项目:CopyNet    作者:MultiPath    | 项目源码 | 文件源码
def init_logging(logfile):
    formatter = logging.Formatter('%(asctime)s [%(levelname)s] %(module)s: %(message)s',
                                  datefmt='%m/%d/%Y %H:%M:%S'   )
    fh = logging.FileHandler(logfile)
    # ch = logging.StreamHandler()

    fh.setFormatter(formatter)
    # ch.setFormatter(formatter)
    # fh.setLevel(logging.INFO)
    # ch.setLevel(logging.INFO)
    # logging.getLogger().addHandler(ch)
    logging.getLogger().addHandler(fh)
    logging.getLogger().setLevel(logging.INFO)

    return logging

# prepare logging.
项目:bay    作者:eventbrite    | 项目源码 | 文件源码
def __attrs_post_init__(self):
        self.logger = logging.getLogger('build_logger')
        self.logger.setLevel(logging.INFO)

        # Close all old logging handlers
        if self.logger.handlers:
            [handler.close() for handler in self.logger.handlers]
            self.logger.handlers = []

        # Add build log file handler
        file_handler = logging.FileHandler(self.logfile_name)
        self.logger.addHandler(file_handler)

        # Optionally add task (console) log handler
        self.task = Task(
            "Building {}".format(CYAN(self.container.name)),
            parent=self.parent_task,
            collapse_if_finished=True,
        )
        if self.verbose:
            self.logger.addHandler(TaskExtraInfoHandler(self.task))
项目:monasca-transform    作者:openstack    | 项目源码 | 文件源码
def init_logger(logger_name):

        # initialize logger
        log = logging.getLogger(logger_name)
        _h = logging.FileHandler('%s/%s' % (
            cfg.CONF.service.service_log_path,
            cfg.CONF.service.service_log_filename))
        _h.setFormatter(logging.Formatter("'%(asctime)s - %(pathname)s:"
                                          "%(lineno)s - %(levelname)s"
                                          " - %(message)s'"))
        log.addHandler(_h)
        if cfg.CONF.service.enable_debug_log_entries:
            log.setLevel(logging.DEBUG)
        else:
            log.setLevel(logging.INFO)

        return log
项目:txt2evernote    作者:Xunius    | 项目源码 | 文件源码
def reset_logpath(logpath):
    """
    Reset logpath to path from command line
    """
    global logger

    if not logpath:
        return

    # remove temporary log file if it's empty
    if os.path.isfile(def_logpath):
        if os.path.getsize(def_logpath) == 0:
            os.remove(def_logpath)

    # save previous handlers
    handlers = logger.handlers

    # remove old handlers
    for handler in handlers:
        logger.removeHandler(handler)

    # try to set new file handler
    handler = logging.FileHandler(logpath)
    handler.setFormatter(formatter)
    logger.addHandler(handler)
项目:safetyculture-sdk-python    作者:SafetyCulture    | 项目源码 | 文件源码
def configure_logging(self):
        """
        Configure logging to log to std output as well as to log file
        """
        log_level = logging.DEBUG

        log_filename = datetime.now().strftime('%Y-%m-%d') + '.log'
        sp_logger = logging.getLogger('sp_logger')
        sp_logger.setLevel(log_level)
        formatter = logging.Formatter('%(asctime)s : %(levelname)s : %(message)s')

        fh = logging.FileHandler(filename=self.log_dir + log_filename)
        fh.setLevel(log_level)
        fh.setFormatter(formatter)
        sp_logger.addHandler(fh)

        sh = logging.StreamHandler(sys.stdout)
        sh.setLevel(log_level)
        sh.setFormatter(formatter)
        sp_logger.addHandler(sh)
项目:IotCenter    作者:panjanek    | 项目源码 | 文件源码
def configureLogging(level, console, file):
    logger = logging.getLogger()    
    logger.setLevel(level)
    formatter = logging.Formatter('%(asctime)s  %(levelname)s   %(message)s')
    if console:
        cons = logging.StreamHandler()
        cons.setLevel(level)
        cons.setFormatter(formatter)
        logger.addHandler(cons)
        print("logging to console")

    if file:              
        f = logging.FileHandler(file)        
        f.setLevel(level)
        f.setFormatter(formatter)
        logger.addHandler(f)        
        print("logging to file {0}".format(file))
项目:kinect-2-libras    作者:inessadl    | 项目源码 | 文件源码
def emit(self, record):
        """
        Emit a record.

        First check if the underlying file has changed, and if it
        has, close the old stream and reopen the file to get the
        current stream.
        """
        if not os.path.exists(self.baseFilename):
            stat = None
            changed = 1
        else:
            stat = os.stat(self.baseFilename)
            changed = (stat[ST_DEV] != self.dev) or (stat[ST_INO] != self.ino)
        if changed and self.stream is not None:
            self.stream.flush()
            self.stream.close()
            self.stream = self._open()
            if stat is None:
                stat = os.stat(self.baseFilename)
            self.dev, self.ino = stat[ST_DEV], stat[ST_INO]
        logging.FileHandler.emit(self, record)
项目:VxFuzz    作者:yformaggio    | 项目源码 | 文件源码
def logger(level, name, logfile):
        """ Create and configure file and console logging.
        :param level: console debugging level only.
        :param name: logger name
        :param logfile: log destination file name
        :return: configured logging object
        """
        logger = logging.getLogger(name)
        console_handler = logging.StreamHandler()
        console_handler.setLevel(level)
        file_handler = logging.FileHandler(logfile)
        file_handler.setLevel(logging.DEBUG)
        console_formatter = logging.Formatter("[%(levelname)s] %(message)s")
        file_formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
        console_handler.setFormatter(console_formatter)
        file_handler.setFormatter(file_formatter)
        logger.addHandler(console_handler)
        logger.addHandler(file_handler)

        return logger
项目:virtualbmc    作者:umago    | 项目源码 | 文件源码
def __init__(self, debug=False, logfile=None):
        logging.Logger.__init__(self, 'VirtualBMC')
        try:
            if logfile is not None:
                self.handler = logging.FileHandler(logfile)
            else:
                self.handler = logging.StreamHandler()

            formatter = logging.Formatter(DEFAULT_LOG_FORMAT)
            self.handler.setFormatter(formatter)
            self.addHandler(self.handler)

            if debug:
                self.setLevel(logging.DEBUG)
            else:
                self.setLevel(logging.INFO)

        except IOError, e:
            if e.errno == errno.EACCES:
                pass
项目:FenicsSolver    作者:qingfengxia    | 项目源码 | 文件源码
def set_logger(self, s):
        logger = logging.getLogger(self.__class__.__name__)
        # create console handler and set level to debug
        if ('logging_file' not in s) or (s['logging_file'] == None):
            fh = logging.StreamHandler()
        else:
            fh = logging.FileHandler(s['logging_file'])
        if 'logging_level' in s:
            fh.setLevel(s['logging_level'])
        else:
            fh.setLevel(logging.DEBUG)
        # create formatter
        formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
        fh.setFormatter(formatter)

        # add console stdout or log file to logger
        logger.addHandler(fh)
        self.logger = logger  # usage: self.logger.debug(msg)
项目:oriole-service    作者:zhouxiaoxiang    | 项目源码 | 文件源码
def logger(level='DEBUG', name=""):
    fmt = '[%(module)s] %(asctime)s %(levelname)-7.7s %(message)s'
    dfmt = '%Y-%m-%d %H:%M:%S'
    level = getattr(logging, level, DEBUG)

    logger = getLogger('services')
    logger.setLevel(level)
    fmter = Formatter(fmt, dfmt)
    del logger.handlers[:]

    if name:
        fh = FileHandler(name)
        fh.setLevel(level)
        fh.setFormatter(fmter)
        logger.addHandler(fh)

    ch = StreamHandler()
    ch.setLevel(level)
    ch.setFormatter(fmter)
    logger.addHandler(ch)
    logger.propagate = False

    return logger
项目:functest    作者:opnfv    | 项目源码 | 文件源码
def __init__(self, **kwargs):
        super(Feature, self).__init__(**kwargs)
        self.result_file = "{}/{}.log".format(
            CONST.__getattribute__('dir_results'), self.case_name)
        try:
            module = kwargs['run']['module']
            self.logger = logging.getLogger(module)
        except KeyError:
            self.__logger.warning(
                "Cannot get module name %s. Using %s as fallback",
                kwargs, self.case_name)
            self.logger = logging.getLogger(self.case_name)
        handler = logging.StreamHandler()
        handler.setLevel(logging.WARN)
        self.logger.addHandler(handler)
        handler = logging.FileHandler(self.result_file)
        handler.setLevel(logging.DEBUG)
        self.logger.addHandler(handler)
        formatter = logging.Formatter(
            '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
        handler.setFormatter(formatter)
        self.logger.addHandler(handler)
项目:supremm    作者:ubccr    | 项目源码 | 文件源码
def setuplogger(consolelevel, filename=None, filelevel=None):
    """ setup the python root logger to log to the console with defined log
        level. Optionally also log to file with the provided level """

    if filelevel == None:
        filelevel = consolelevel

    if sys.version.startswith("2.7"):
        logging.captureWarnings(True)

    rootlogger = logging.getLogger()
    rootlogger.setLevel(min(consolelevel, filelevel))

    formatter = logging.Formatter('%(asctime)s.%(msecs)03d [%(levelname)s] %(message)s', datefmt='%Y-%m-%dT%H:%M:%S')

    if filename != None:
        filehandler = logging.FileHandler(filename)
        filehandler.setLevel(filelevel)
        filehandler.setFormatter(formatter)
        rootlogger.addHandler(filehandler)

    consolehandler = logging.StreamHandler()
    consolehandler.setLevel(consolelevel)
    consolehandler.setFormatter(formatter)
    rootlogger.addHandler(consolehandler)
项目:CopyNet    作者:MultiPath    | 项目源码 | 文件源码
def init_logging(logfile):
    formatter = logging.Formatter('%(asctime)s [%(levelname)s] %(module)s: %(message)s',
                                  datefmt='%m/%d/%Y %H:%M:%S'   )
    fh = logging.FileHandler(logfile)
    # ch = logging.StreamHandler()

    fh.setFormatter(formatter)
    # ch.setFormatter(formatter)
    # fh.setLevel(logging.INFO)
    # ch.setLevel(logging.INFO)
    # logging.getLogger().addHandler(ch)
    logging.getLogger().addHandler(fh)
    logging.getLogger().setLevel(logging.INFO)
    return logging

# prepare logging.
项目:CopyNet    作者:MultiPath    | 项目源码 | 文件源码
def init_logging(logfile):
    formatter = logging.Formatter('%(asctime)s [%(levelname)s] %(module)s: %(message)s',
                                  datefmt='%m/%d/%Y %H:%M:%S'   )
    fh = logging.FileHandler(logfile)
    # ch = logging.StreamHandler()

    fh.setFormatter(formatter)
    # ch.setFormatter(formatter)
    # fh.setLevel(logging.INFO)
    # ch.setLevel(logging.INFO)
    # logging.getLogger().addHandler(ch)
    logging.getLogger().addHandler(fh)
    logging.getLogger().setLevel(logging.INFO)

    return logging

# prepare logging.
项目:CopyNet    作者:MultiPath    | 项目源码 | 文件源码
def init_logging(logfile):
    formatter = logging.Formatter('%(asctime)s [%(levelname)s] %(module)s: %(message)s',
                                  datefmt='%m/%d/%Y %H:%M:%S'   )
    fh = logging.FileHandler(logfile)
    # ch = logging.StreamHandler()

    fh.setFormatter(formatter)
    # ch.setFormatter(formatter)
    # fh.setLevel(logging.INFO)
    # ch.setLevel(logging.INFO)
    # logging.getLogger().addHandler(ch)
    logging.getLogger().addHandler(fh)
    logging.getLogger().setLevel(logging.INFO)

    return logging

# prepare logging.
项目:CopyNet    作者:MultiPath    | 项目源码 | 文件源码
def init_logging(logfile):
    formatter = logging.Formatter('%(asctime)s [%(levelname)s] %(module)s: %(message)s',
                                  datefmt='%m/%d/%Y %H:%M:%S'   )
    fh = logging.FileHandler(logfile)
    # ch = logging.StreamHandler()

    fh.setFormatter(formatter)
    # ch.setFormatter(formatter)
    # fh.setLevel(logging.INFO)
    # ch.setLevel(logging.INFO)
    # logging.getLogger().addHandler(ch)
    logging.getLogger().addHandler(fh)
    logging.getLogger().setLevel(logging.INFO)

    return logging

# prepare logging.
项目:CopyNet    作者:MultiPath    | 项目源码 | 文件源码
def init_logging(logfile):
    formatter = logging.Formatter('%(asctime)s [%(levelname)s] %(module)s: %(message)s',
                                  datefmt='%m/%d/%Y %H:%M:%S'   )
    fh = logging.FileHandler(logfile)
    # ch = logging.StreamHandler()

    fh.setFormatter(formatter)
    # ch.setFormatter(formatter)
    # fh.setLevel(logging.INFO)
    # ch.setLevel(logging.INFO)
    # logging.getLogger().addHandler(ch)
    logging.getLogger().addHandler(fh)
    logging.getLogger().setLevel(logging.INFO)

    return logging

# prepare logging.
项目:CopyNet    作者:MultiPath    | 项目源码 | 文件源码
def init_logging(logfile):
    formatter = logging.Formatter('%(asctime)s [%(levelname)s] %(module)s: %(message)s',
                                  datefmt='%m/%d/%Y %H:%M:%S'   )
    fh = logging.FileHandler(logfile)
    # ch = logging.StreamHandler()

    fh.setFormatter(formatter)
    # ch.setFormatter(formatter)
    # fh.setLevel(logging.INFO)
    # ch.setLevel(logging.INFO)
    # logging.getLogger().addHandler(ch)
    logging.getLogger().addHandler(fh)
    logging.getLogger().setLevel(logging.INFO)

    return logging

# prepare logging.
项目:CopyNet    作者:MultiPath    | 项目源码 | 文件源码
def init_logging(logfile):
    formatter = logging.Formatter('%(asctime)s [%(levelname)s] %(module)s: %(message)s',
                                  datefmt='%m/%d/%Y %H:%M:%S'   )
    fh = logging.FileHandler(logfile)
    # ch = logging.StreamHandler()

    fh.setFormatter(formatter)
    # ch.setFormatter(formatter)
    # fh.setLevel(logging.INFO)
    # ch.setLevel(logging.INFO)
    # logging.getLogger().addHandler(ch)
    logging.getLogger().addHandler(fh)
    logging.getLogger().setLevel(logging.INFO)

    return logging

# prepare logging.
项目:CopyNet    作者:MultiPath    | 项目源码 | 文件源码
def init_logging(logfile):
    formatter = logging.Formatter('%(asctime)s [%(levelname)s] %(module)s: %(message)s',
                                  datefmt='%m/%d/%Y %H:%M:%S'   )
    fh = logging.FileHandler(logfile)
    # ch = logging.StreamHandler()

    fh.setFormatter(formatter)
    # ch.setFormatter(formatter)
    # fh.setLevel(logging.INFO)
    # ch.setLevel(logging.INFO)
    # logging.getLogger().addHandler(ch)
    logging.getLogger().addHandler(fh)
    logging.getLogger().setLevel(logging.INFO)

    return logging

# prepare logging.
项目:CopyNet    作者:MultiPath    | 项目源码 | 文件源码
def init_logging(logfile):
    formatter = logging.Formatter('%(asctime)s [%(levelname)s] %(module)s: %(message)s',
                                  datefmt='%m/%d/%Y %H:%M:%S'   )
    fh = logging.FileHandler(logfile)
    # ch = logging.StreamHandler()

    fh.setFormatter(formatter)
    # ch.setFormatter(formatter)
    # fh.setLevel(logging.INFO)
    # ch.setLevel(logging.INFO)
    # logging.getLogger().addHandler(ch)
    logging.getLogger().addHandler(fh)
    logging.getLogger().setLevel(logging.INFO)

    return logging


# prepare logging.
项目:CopyNet    作者:MultiPath    | 项目源码 | 文件源码
def init_logging(logfile):
    formatter = logging.Formatter('%(asctime)s [%(levelname)s] %(module)s: %(message)s',
                                  datefmt='%m/%d/%Y %H:%M:%S'   )
    fh = logging.FileHandler(logfile)
    # ch = logging.StreamHandler()

    fh.setFormatter(formatter)
    # ch.setFormatter(formatter)
    # fh.setLevel(logging.INFO)
    # ch.setLevel(logging.INFO)
    # logging.getLogger().addHandler(ch)
    logging.getLogger().addHandler(fh)
    logging.getLogger().setLevel(logging.INFO)

    return logging

# prepare logging.
项目:geekcloud    作者:Mr-Linus    | 项目源码 | 文件源码
def set_log(level, filename='jumpserver.log'):
    """
    return a log file object
    ??????log??
    """
    log_file = os.path.join(LOG_DIR, filename)
    if not os.path.isfile(log_file):
        os.mknod(log_file)
        os.chmod(log_file, 0777)
    log_level_total = {'debug': logging.DEBUG, 'info': logging.INFO, 'warning': logging.WARN, 'error': logging.ERROR,
                       'critical': logging.CRITICAL}
    logger_f = logging.getLogger('jumpserver')
    logger_f.setLevel(logging.DEBUG)
    fh = logging.FileHandler(log_file)
    fh.setLevel(log_level_total.get(level, logging.DEBUG))
    formatter = logging.Formatter('%(asctime)s - %(filename)s - %(levelname)s - %(message)s')
    fh.setFormatter(formatter)
    logger_f.addHandler(fh)
    return logger_f
项目:Dict-O-nator    作者:theawless    | 项目源码 | 文件源码
def setup_logger():
    # setting format of log
    formatter = logging.Formatter('%(threadName)s - %(levelname)s - %(message)s')
    logger.setLevel(logging.DEBUG)
    # file location
    debug_log = LOG_DIR_PATH + 'log.txt'

    # adding handler for console logs
    sh = logging.StreamHandler()
    sh.setFormatter(formatter)
    logger.addHandler(sh)

    # adding handler for file logs
    fh = logging.FileHandler(debug_log)
    fh.setFormatter(formatter)
    logger.addHandler(fh)
项目:ComplexityEstimator    作者:marwin1991    | 项目源码 | 文件源码
def init_logger(name):
    global loggers

    if loggers.get(name):
        return loggers.get(name)
    else:
        logger = logging.getLogger(name)
        logger.setLevel(logging.DEBUG)

        handler = logging.FileHandler('calc_complex_logs.log')
        handler.setLevel(logging.DEBUG)

        handler.setFormatter(logging.Formatter(
            '%(asctime)s - %(name)s - %(levelname)s - %(message)s'))
        logger.addHandler(handler)
        loggers[name]=logger
        return logger
项目:iosxr-ansible    作者:ios-xr    | 项目源码 | 文件源码
def init_logging (logfile):
    # Initialize the logging infra and add a handler
    logger = logging.getLogger ("ydk") 
    logger.setLevel (logging.DEBUG)

    # create file handler             
    fh = logging.FileHandler (logfile)
    fh.setLevel (logging.DEBUG)

    # create a console logger too
    ch = logging.StreamHandler ()
    ch.setLevel (logging.ERROR)

    # add the handlers to the logger
    logger.addHandler (fh)
    logger.addHandler (ch)
项目:sketal    作者:vk-brain    | 项目源码 | 文件源码
def init_logger(self, logger):
        if not logger:
            logger = logging.Logger("sketal", level=logging.DEBUG if self.settings.DEBUG else logging.INFO)

        formatter = logging.Formatter(fmt=u'%(filename)-10s [%(asctime)s] %(levelname)-8s: %(message)s',
                                      datefmt='%y.%m.%d %H:%M:%S')

        file_handler = logging.FileHandler('logs.txt')
        file_handler.setLevel(logging.DEBUG)
        file_handler.setFormatter(formatter)

        self.logger_file = file_handler

        stream_handler = logging.StreamHandler()
        stream_handler.setLevel(level=logging.DEBUG if self.settings.DEBUG else logging.INFO)
        stream_handler.setFormatter(formatter)

        logger.addHandler(file_handler)
        logger.addHandler(stream_handler)

        self.logger = logger
项目:discover-books    作者:shispt    | 项目源码 | 文件源码
def get_logger(name, filename, level=logging.DEBUG, fmt=None):
    logger = logging.Logger(name)

    fmt = fmt or '%(asctime)s-%(name)s-%(levelname)-10s%(message)s'
    formatter = logging.Formatter(fmt=fmt, datefmt='%Y-%m-%d %H:%M:%S')

    stream_handler = logging.StreamHandler()
    stream_handler.setFormatter(formatter)

    file_handler = logging.FileHandler(filename)
    file_handler.setFormatter(formatter)

    logger.addHandler(stream_handler)
    logger.addHandler(file_handler)

    logger.setLevel(level)

    return logger
项目:vspheretools    作者:devopshq    | 项目源码 | 文件源码
def EnableTestLogger(testLogFileName, parentLoggerHandler=LOGGER, formatting=formatter):
    """
    Start logging for given parent logger handler and test log-file name. Possible, you can use logger formatter.
    Function return new open Logger handler.
    """
    testCaseLogHandler = logging.FileHandler(testLogFileName)  # log handler

    if formatting:
        testCaseLogHandler.setFormatter(formatting)  # set given log formatting

    else:
        testCaseLogHandler.setFormatter(formatter)  # set default log formatting

    parentLoggerHandler.addHandler(testCaseLogHandler)  # init test log

    return testCaseLogHandler
项目:ceph-medic    作者:ceph    | 项目源码 | 文件源码
def setup(config=None):
    root_logger = logging.getLogger()
    log_path = config.get_safe('global', '--log-path', '.')
    if not os.path.exists(log_path):
        raise RuntimeError('configured ``--log-path`` value does not exist: %s' % log_path)
    date = datetime.strftime(datetime.utcnow(), '%Y-%m-%d')
    log_file = os.path.join(log_path, 'ceph-medic-%s.log' % date)

    root_logger.setLevel(logging.DEBUG)

    # File Logger
    fh = logging.FileHandler(log_file)
    fh.setLevel(logging.DEBUG)
    fh.setFormatter(logging.Formatter(FILE_FORMAT))

    root_logger.addHandler(fh)
项目:NodeDefender    作者:CTSNE    | 项目源码 | 文件源码
def CreateLogging(app = None):
    app = app or CreateApp()
    if not app.config['LOGGING']:
        loggHandler = logging.StreamHandler(sys.stdout)
    else:
        if app.config['LOGGING_TYPE'] == 'local':
            loggHandler = logging.FileHandler(app.config['LOGGING_NAME'])
        elif app.config['LOGGING_TYPE'] == 'syslog':
            loggHandler = logging.handler.\
                    SysLogHandler(address = (app.config['LOGGING_SERVER'],
                                             int(app.config['LOGGING_PORT'])))
    level = NodeDefender.config.logging.level()
    if level:
        loggHandler.setLevel(level.upper())
    else:
        loggHandler.setLevel("DEBUG")

    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    loggHandler.setFormatter(formatter)

    logger = logging.getLogger("NodeDefender")
    logger.setLevel(logging.INFO)
    logger.addHandler(loggHandler)
    return logger, loggHandler
项目:Software-Architecture-with-Python    作者:PacktPublishing    | 项目源码 | 文件源码
def create_logger(app_name, logfilename=None, level=logging.INFO,
                  console=False, syslog=False):
    """ Build and return a custom logger. Accepts the application name,
    log filename, loglevel and console logging toggle and syslog toggle """

    log=logging.getLogger(app_name)
    log.setLevel(logging.DEBUG)
    # Add file handler
    if logfilename != None:
        log.addHandler(logging.FileHandler(logfilename))

    if syslog:
        log.addHandler(logging.handlers.SysLogHandler(address='/dev/log'))

    if console:
        log.addHandler(logging.StreamHandler())

    # Add formatter
    for handle in log.handlers:
        formatter = logging.Formatter('%(asctime)s : %(levelname)-8s - %(message)s',
                                      datefmt='%Y-%m-%d %H:%M:%S')
        handle.setFormatter(formatter)

    return log
项目:rltk    作者:usc-isi-i2    | 项目源码 | 文件源码
def update_logging_settings(self, file_path=None, level=None, format=None):
        """
        Update global logging. If None is set to the arguments, it will keep the previous setting.

        Args:
            file_path (str): It is Initialized to 'log.log'.
            level (str): It can be 'error', 'warning' or 'info'. It is Initialized to 'error'.
            format (str): It is Initialized to '%(asctime)s %(levelname)s %(message)s'.
        """

        LOGGING_STRING_MAP = {'info': logging.INFO, 'warning': logging.WARNING, 'error': logging.ERROR}
        if file_path is not None:
            self._logger_config['file_path'] = self._get_abs_path(file_path)
        if level is not None:
            self._logger_config['level'] = level
        if format is not None:
            self._logger_config['format'] = format

        logger = logging.getLogger(Configuration.LOGGER_NAME)
        log_file = logging.FileHandler(self._logger_config['file_path'])
        logger.addHandler(log_file)
        log_file.setFormatter(logging.Formatter(self._logger_config['format']))
        logger.setLevel(LOGGING_STRING_MAP[self._logger_config['level']])
        self._logger = logger
项目:Supply-demand-forecasting    作者:LevinJ    | 项目源码 | 文件源码
def __init__(self):
        TFModel.__init__(self)
        PrepareData.__init__(self)
        EarlyStopMonitor.__init__(self)
        self.num_steps = 30000
        self.batch_size = 128
        self.early_stopping_rounds = None
        self.summaries_dir = './logs/didi'
        self.dropout= 0.9
        self.usedFeatures = [101,102,103,104,105,     #106,107, 
                             201, 204, #205,206, 203,
                             301,
                             401,402,
                             501,502,503,      #504,505,506,507,
                            601,602,603,604,605,606,
                             8801,8802
                             ]
        self.train_validation_foldid = -2 #0.415
#         self.train_validation_foldid = -1 #0.425
#         self.train_validation_foldid = -3 #0.430
        logging.getLogger().addHandler(logging.FileHandler('logs/didnerual.log', mode='w'))
        return
项目:Supply-demand-forecasting    作者:LevinJ    | 项目源码 | 文件源码
def __init__(self):
        root = logging.getLogger()
        root.setLevel(logging.DEBUG)
        root.addHandler(logging.StreamHandler(sys.stdout))
        root.addHandler(logging.FileHandler('logs/forwardfeatureselection_knn.log', mode='w'))
        clfDict = {1: GrientBoostingModel, 2:KNNModel, 3: DidiXGBoostModel}
        self.clf =  clfDict[2]()
#         self.result = []
#         self.featureList = [101,102, 201,502]
        self.featureList =  [101,102,103,104,105,106,107, 
                             201, 203,204,205,206,
                             301,
                             401,402,
                             501,502,503,504,505,506,507,
                             601,602,603,604,605,606,
                             8801,8802
                             ]

        return
项目:scm-workbench    作者:barry-scott    | 项目源码 | 文件源码
def emit(self, record):
        """
        Emit a record.

        Output the record to the file, catering for rollover as described
        in setRollover().
        """
        if self.maxBytes > 0:                   # are we rolling over?
            msg = "%s\n" % self.format(record)
            try:
                self.stream.seek(0, 2)  #due to non-posix-compliant Windows feature
                if self.stream.tell() + len(msg) >= self.maxBytes:
                    self.doRollover()

            except ValueError:
                # on Windows we get "ValueError: I/O operation on closed file"
                # when a second copy of workbench is run
                self.doRollover()

        logging.FileHandler.emit(self, record)
项目:tie2misp    作者:DCSO    | 项目源码 | 文件源码
def init_logger(logPath, fileName, logLvl, consoleLog, fileLog):

        logger = logging.getLogger()
        logger.setLevel(logLvl)
        formatter = logging.Formatter('%(asctime)s [%(levelname)-5.5s]  %(message)s')

        consoleHandler = logging.StreamHandler(sys.stdout)

        consoleHandler.setFormatter(formatter)
        logger.addHandler(consoleHandler)

        if consoleLog is False:
            consoleHandler.setLevel(logLvl)
        else:
            consoleHandler.setLevel(100)

        if fileLog is False:
            fileHandler = logging.FileHandler("{0}/{1}.log".format(logPath, fileName))
            fileHandler.setFormatter(formatter)
            fileHandler.setLevel(logLvl)
            logger.addHandler(fileHandler)
项目:simLAB    作者:kamwar    | 项目源码 | 文件源码
def __init__(self, type=types.TYPE_USIM, logLevel=logging.INFO):
        dir = os.path.dirname(__file__)
        resultFile = dir + "/../sim_soft.log"
        FORMATTER = logging.Formatter(fmt='%(asctime)s %(message)s', datefmt='%H:%M:%S')
        fileHndl = logging.FileHandler(resultFile, mode='w')
        fileHndl.setFormatter(FORMATTER)
        fileHndl.setLevel(logLevel)
        logger = logging.getLogger("sim_soft")
        #dont't propagate to root logger
        logger.propagate=False
        logger.handlers = []
        logger.setLevel(logLevel)
        logger.addHandler(fileHndl)
        self.logging = logger
        self.readers = []
        self.simType = type
项目:seglink    作者:bgshih    | 项目源码 | 文件源码
def setup_logger(log_file_path):
  """
  Setup a logger that simultaneously output to a file and stdout
  ARGS
    log_file_path: string, path to the logging file
  """
  # logging settings
  log_formatter = logging.Formatter("%(asctime)s [%(levelname)-5.5s]  %(message)s")
  root_logger = logging.getLogger()
  root_logger.setLevel(logging.DEBUG)
  # file handler
  log_file_handler = logging.FileHandler(log_file_path)
  log_file_handler.setFormatter(log_formatter)
  root_logger.addHandler(log_file_handler)
  # stdout handler
  log_stream_handler = logging.StreamHandler(sys.stdout)
  log_stream_handler.setFormatter(log_formatter)
  root_logger.addHandler(log_stream_handler)

  logging.info('Log file is %s' % log_file_path)
项目:waterflowers    作者:chaodalong    | 项目源码 | 文件源码
def set_logger_handler(app):
    """
    ????handler
    :param app:
    :return:
    """
    import logging
    from logging import Formatter
    from logging import FileHandler
    filehandler = FileHandler('/tmp/py.log', 'a+')
    filehandler.setLevel(logging.DEBUG)
    filehandler.setFormatter(Formatter(
        '%(asctime)s %(levelname)s: %(message)s '
        '[in %(pathname)s:%(lineno)d]'
    ))
    app.logger.addHandler(filehandler)
项目:virtualbmc    作者:openstack    | 项目源码 | 文件源码
def __init__(self, debug=False, logfile=None):
        logging.Logger.__init__(self, 'VirtualBMC')
        try:
            if logfile is not None:
                self.handler = logging.FileHandler(logfile)
            else:
                self.handler = logging.StreamHandler()

            formatter = logging.Formatter(DEFAULT_LOG_FORMAT)
            self.handler.setFormatter(formatter)
            self.addHandler(self.handler)

            if debug:
                self.setLevel(logging.DEBUG)
            else:
                self.setLevel(logging.INFO)

        except IOError as e:
            if e.errno == errno.EACCES:
                pass
项目:roguelike-tutorial    作者:Wolfenswan    | 项目源码 | 文件源码
def setup_logging(debugging):
    """ configures logging """

    # configure logging
    formatting = '%(asctime)s | %(levelname)s |  %(funcName)s | %(message)s'
    log_file = 'logs/{0}.log'.format(time.strftime("%d.%m.%Y %H-%M"))
    logging.basicConfig(level=logging.DEBUG, format=formatting)

    # disable all non-error messages if not debugging
    if not debugging:
        logging.disable(logging.DEBUG)

    # setup output streams
    rootLogger = logging.getLogger()

    # file output
    logFormatter = logging.Formatter(formatting)
    fileHandler = logging.FileHandler("{0}".format(log_file))
    fileHandler.setFormatter(logFormatter)
    rootLogger.addHandler(fileHandler)

    # terminal output
    # consoleHandler = logging.StreamHandler()
    # consoleHandler.setFormatter(logFormatter)
    # rootLogger.addHandler(consoleHandler)
项目:PomodoroBot    作者:VicenteRD    | 项目源码 | 文件源码
def init_logger():
    """ Instantiates and sets up the logger, if it's not already set up.
    """

    if _logger.ready:
        return

    log_fmt = logging.Formatter(
        fmt='[{asctime}][{levelname:^7}] {message}',
        datefmt='%m/%d | %H:%M:%S', style='{')

    file_handler = logging.FileHandler(filename='pomodorobot.log',
                                       encoding='utf8', mode='w')
    term_handler = logging.StreamHandler(sys.stdout)

    file_handler.setFormatter(log_fmt)
    term_handler.setFormatter(log_fmt)
    _logger.logger.addHandler(file_handler)
    _logger.logger.addHandler(term_handler)

    _logger.logger.setLevel(logging.INFO)

    _logger.ready = True
项目:seq2seq    作者:eske    | 项目源码 | 文件源码
def create_logger(log_file=None):
    """
    Initialize global logger and return it.

    :param log_file: log to this file, or to standard output if None
    :return: created logger
    """
    formatter = logging.Formatter(fmt='%(asctime)s %(message)s', datefmt='%m/%d %H:%M:%S')
    if log_file is not None:
        os.makedirs(os.path.dirname(log_file), exist_ok=True)
        handler = logging.FileHandler(log_file)
        handler.setFormatter(formatter)
        logger = logging.getLogger(__name__)
        logger.addHandler(handler)

    handler = logging.StreamHandler()
    handler.setFormatter(formatter)
    logger = logging.getLogger(__name__)
    logger.addHandler(handler)
    return logger
项目:mesos-toolbox    作者:AppMinistry    | 项目源码 | 文件源码
def get_log(program):
        Utils.source_provisioner_env()

        log = logging.getLogger(os.path.basename(program))
        log.setLevel(logging.INFO)
        # create handlers
        file_handler    = logging.FileHandler(os.environ['PROVISIONING_LOG_FILE'])
        file_handler.setLevel(logging.INFO)
        console_handler = logging.StreamHandler(sys.stdout)
        console_handler.setLevel(logging.INFO)
        # create loggin format:
        formatter = logging.Formatter("[@:%(created)f]:[%(name)s]: %(message)s")
        # set formatter
        file_handler.setFormatter(formatter)
        console_handler.setFormatter(formatter)
        # add handlers
        log.addHandler(file_handler)
        log.addHandler(console_handler)
        # return the log
        return log
项目:simple-python-scripts    作者:jiegzhan    | 项目源码 | 文件源码
def log_to_both_file_and_console():
    root_logger = logging.getLogger()
    root_logger.setLevel(logging.DEBUG)

    formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')

    # Set up log for file.
    file_handler = logging.FileHandler('log_info.txt')
    file_handler.setLevel(logging.DEBUG)
    file_handler.setFormatter(formatter)
    root_logger.addHandler(file_handler)

    # Set up log for terminal.
    console_handler = logging.StreamHandler()
    console_handler.setLevel(logging.DEBUG)
    console_handler.setFormatter(formatter)
    root_logger.addHandler(console_handler)

    root_logger.debug('This is a log_debug message in the program.')
    root_logger.error('This is a log_error message in the  program.')

# ------ Log to different logging levels. ------
项目:logger    作者:d3QUone    | 项目源码 | 文件源码
def __init__(self, name):
        self.logger_id = str(uuid4())
        self.filename = os.path.join(self.BASE_DIR, "{}.log".format(name))
        if not os.path.isdir(self.BASE_DIR):
            os.mkdir(self.BASE_DIR)

        self.console_formatter = ColorizedFormatter(self.DEFAULT_FORMAT, use_color=True)
        self.syslog = logging.StreamHandler(sys.stdout)
        self.syslog.setFormatter(self.console_formatter)
        self.syslog.setLevel(self.DEFAULT_LEVEL)

        self.file_formatter = ColorizedFormatter(self.DEFAULT_FORMAT, use_color=False)
        self.file_handler = logging.FileHandler(self.filename, encoding="utf8")
        self.file_handler.setFormatter(self.file_formatter)
        self.file_handler.setLevel(self.DEFAULT_LEVEL)

        self.logger = logging.getLogger("{}-{}".format(self.logger_id, self.filename))
        self.logger.setLevel(self.DEFAULT_LEVEL)
        self.logger.addHandler(self.syslog)
        self.logger.addHandler(self.file_handler)