Python logging.FileHandler() Examples

The following are code examples for showing how to use logging.FileHandler(). They are extracted from open source Python projects. You can vote up the examples you like or vote down the exmaples you don't like. You can also save this page to your account.

Example 1
Project: safetyculture-sdk-python   Author: SafetyCulture   File: import_grs.py    (license) View Source Project 15 votes vote down vote up
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) 
Example 2
Project: test-infra   Author: istio   File: bootstrap.py    (license) View Source Project 11 votes vote down vote up
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 
Example 3
Project: CopyNet   Author: MultiPath   File: lcsts_test.py    (MIT License) View Source Project 7 votes vote down vote up
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. 
Example 4
Project: NodeDefender   Author: CTSNE   File: factory.py    (license) View Source Project 7 votes vote down vote up
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 
Example 5
Project: Software-Architecture-with-Python   Author: PacktPublishing   File: custom_logger.py    (license) View Source Project 7 votes vote down vote up
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 
Example 6
Project: rltk   Author: usc-isi-i2   File: core.py    (license) View Source Project 7 votes vote down vote up
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 
Example 7
Project: bay   Author: eventbrite   File: build.py    (license) View Source Project 7 votes vote down vote up
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)) 
Example 8
Project: monasca-transform   Author: openstack   File: log_utils.py    (Apache License 2.0) View Source Project 6 votes vote down vote up
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 
Example 9
Project: txt2evernote   Author: Xunius   File: gnsync.py    (GNU General Public License v3.0) View Source Project 6 votes vote down vote up
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) 
Example 10
Project: safetyculture-sdk-python   Author: SafetyCulture   File: safetypy.py    (license) View Source Project 6 votes vote down vote up
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) 
Example 11
Project: IotCenter   Author: panjanek   File: iotcommon.py    (MIT License) View Source Project 6 votes vote down vote up
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)) 
Example 12
Project: kinect-2-libras   Author: inessadl   File: handlers.py    (Apache License 2.0) View Source Project 6 votes vote down vote up
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) 
Example 13
Project: VxFuzz   Author: yformaggio   File: fuzz.py    (GNU General Public License v3.0) View Source Project 6 votes vote down vote up
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 
Example 14
Project: virtualbmc   Author: umago   File: log.py    (Apache License 2.0) View Source Project 6 votes vote down vote up
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 
Example 15
Project: FenicsSolver   Author: qingfengxia   File: SolverBase.py    (GNU Lesser General Public License v3.0) View Source Project 6 votes vote down vote up
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) 
Example 16
Project: oriole-service   Author: zhouxiaoxiang   File: api.py    (license) View Source Project 6 votes vote down vote up
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 
Example 17
Project: functest   Author: opnfv   File: feature.py    (Apache License 2.0) View Source Project 6 votes vote down vote up
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) 
Example 18
Project: supremm   Author: ubccr   File: scripthelpers.py    (GNU Lesser General Public License v3.0) View Source Project 6 votes vote down vote up
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) 
Example 19
Project: CopyNet   Author: MultiPath   File: copynet_input.py    (MIT License) View Source Project 6 votes vote down vote up
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. 
Example 20
Project: CopyNet   Author: MultiPath   File: syn_vest.py    (MIT License) View Source Project 6 votes vote down vote up
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. 
Example 21
Project: CopyNet   Author: MultiPath   File: syntest.py    (MIT License) View Source Project 6 votes vote down vote up
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. 
Example 22
Project: CopyNet   Author: MultiPath   File: bst_vest.py    (MIT License) View Source Project 6 votes vote down vote up
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. 
Example 23
Project: CopyNet   Author: MultiPath   File: lcsts_vest.py    (MIT License) View Source Project 6 votes vote down vote up
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. 
Example 24
Project: CopyNet   Author: MultiPath   File: lcsts_vest_new.py    (MIT License) View Source Project 6 votes vote down vote up
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. 
Example 25
Project: CopyNet   Author: MultiPath   File: weibo_vest.py    (MIT License) View Source Project 6 votes vote down vote up
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. 
Example 26
Project: CopyNet   Author: MultiPath   File: copynet.py    (MIT License) View Source Project 6 votes vote down vote up
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. 
Example 27
Project: CopyNet   Author: MultiPath   File: lcsts_rouge.py    (MIT License) View Source Project 6 votes vote down vote up
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. 
Example 28
Project: CopyNet   Author: MultiPath   File: test_lm.py    (MIT License) View Source Project 6 votes vote down vote up
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. 
Example 29
Project: geekcloud   Author: Mr-Linus   File: api.py    (GNU General Public License v2.0) View Source Project 6 votes vote down vote up
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 
Example 30
Project: Dict-O-nator   Author: theawless   File: setlog.py    (GNU General Public License v3.0) View Source Project 6 votes vote down vote up
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) 
Example 31
Project: ComplexityEstimator   Author: marwin1991   File: logger_init.py    (license) View Source Project 6 votes vote down vote up
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 
Example 32
Project: iosxr-ansible   Author: ios-xr   File: iosxr_show_install_version.py    (license) View Source Project 6 votes vote down vote up
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) 
Example 33
Project: sketal   Author: vk-brain   File: bot.py    (license) View Source Project 6 votes vote down vote up
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 
Example 34
Project: discover-books   Author: shispt   File: utils.py    (license) View Source Project 6 votes vote down vote up
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 
Example 35
Project: vspheretools   Author: devopshq   File: Logger.py    (license) View Source Project 6 votes vote down vote up
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 
Example 36
Project: ceph-medic   Author: ceph   File: log.py    (license) View Source Project 6 votes vote down vote up
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) 
Example 37
Project: Supply-demand-forecasting   Author: LevinJ   File: didineuralmodel.py    (license) View Source Project 6 votes vote down vote up
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 
Example 38
Project: Supply-demand-forecasting   Author: LevinJ   File: forwardfeatureselection.py    (license) View Source Project 6 votes vote down vote up
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 
Example 39
Project: scm-workbench   Author: barry-scott   File: wb_logging.py    (license) View Source Project 6 votes vote down vote up
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) 
Example 40
Project: tie2misp   Author: DCSO   File: loader.py    (license) View Source Project 6 votes vote down vote up
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) 
Example 41
Project: simLAB   Author: kamwar   File: sim_soft_ctrl.py    (license) View Source Project 6 votes vote down vote up
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 
Example 42
Project: seglink   Author: bgshih   File: utils.py    (license) View Source Project 6 votes vote down vote up
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) 
Example 43
Project: waterflowers   Author: chaodalong   File: __init__.py    (license) View Source Project 6 votes vote down vote up
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) 
Example 44
Project: virtualbmc   Author: openstack   File: log.py    (license) View Source Project 6 votes vote down vote up
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 
Example 45
Project: roguelike-tutorial   Author: Wolfenswan   File: log_util.py    (license) View Source Project 6 votes vote down vote up
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) 
Example 46
Project: PomodoroBot   Author: VicenteRD   File: lib.py    (license) View Source Project 6 votes vote down vote up
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 
Example 47
Project: seq2seq   Author: eske   File: utils.py    (license) View Source Project 6 votes vote down vote up
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 
Example 48
Project: mesos-toolbox   Author: AppMinistry   File: utils.py    (license) View Source Project 6 votes vote down vote up
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 
Example 49
Project: simple-python-scripts   Author: jiegzhan   File: logging_not_printing.py    (license) View Source Project 6 votes vote down vote up
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. ------ 
Example 50
Project: logger   Author: d3QUone   File: logger.py    (license) View Source Project 6 votes vote down vote up
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)