Python logging.FileHandler() Examples

The following are code examples for showing how to use logging.FileHandler(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: incubator-spot   Author: apache   File: utilities.py    Apache License 2.0 10 votes vote down vote up
def get_logger(cls,logger_name,create_file=False):

        # create logger for prd_ci
        log = logging.getLogger(logger_name)
        log.setLevel(level=logging.INFO)

        # create formatter and add it to the handlers
        formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')

        if create_file:
                # create file handler for logger.
                fh = logging.FileHandler('SPOT.log')
                fh.setLevel(level=logging.DEBUG)
                fh.setFormatter(formatter)
        # reate console handler for logger.
        ch = logging.StreamHandler()
        ch.setLevel(level=logging.DEBUG)
        ch.setFormatter(formatter)

        # add handlers to logger.
        if create_file:
            log.addHandler(fh)

        log.addHandler(ch)
        return  log 
Example 2
Project: GreenGuard   Author: D3-AI   File: utils.py    MIT License 7 votes vote down vote up
def logging_setup(verbosity=1, logfile=None, logger_name=None):
    logger = logging.getLogger(logger_name)
    log_level = (3 - verbosity) * 10
    fmt = '%(asctime)s - %(process)d - %(levelname)s - %(module)s - %(message)s'
    formatter = logging.Formatter(fmt)
    logger.setLevel(log_level)
    logger.propagate = False

    if logfile:
        file_handler = logging.FileHandler(logfile)
        file_handler.setLevel(logging.DEBUG)
        file_handler.setFormatter(formatter)
        logger.addHandler(file_handler)

    else:
        console_handler = logging.StreamHandler()
        console_handler.setLevel(log_level)
        console_handler.setFormatter(formatter)
        logger.addHandler(console_handler) 
Example 3
Project: Hasami   Author: Lokraan   File: main.py    MIT License 6 votes vote down vote up
def setup_logging(config: dict) -> None:
	logging.getLogger("discord.http").setLevel(logging.WARNING)
	logging.getLogger("discord").setLevel(logging.INFO)

	logger = logging.getLogger()

	level = logging.DEBUG if config["debug"] else logging.INFO

	f_handler = logging.FileHandler(filename="hasami.log", encoding="utf-8", mode="w")
	cl_handler = logging.StreamHandler()

	dt_fmt = "%Y-%m-%d %H:%M:%S"
	out_fmt = "[{asctime}] [{levelname:<6}] {name}: {message}"
	logger_fmt = logging.Formatter(out_fmt, dt_fmt, style="{")

	cl_handler.setFormatter(logger_fmt)
	f_handler.setFormatter(logger_fmt)

	logger.addHandler(cl_handler)
	logger.addHandler(f_handler)
	logger.setLevel(level) 
Example 4
Project: incubator-spot   Author: apache   File: utils.py    Apache License 2.0 6 votes vote down vote up
def get_logger(cls, logger_name, create_file=False):

        # create logger for prd_ci
        log = logging.getLogger(logger_name)
        log.setLevel(level=logging.INFO)

        # create formatter and add it to the handlers
        formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')

        if create_file:
            # create file handler for logger.
            fh = logging.FileHandler('oa.log')
            fh.setLevel(level=logging.DEBUG)
            fh.setFormatter(formatter)
        # reate console handler for logger.
        ch = logging.StreamHandler()
        ch.setLevel(level=logging.DEBUG)
        ch.setFormatter(formatter)

        # add handlers to logger.
        if create_file:
            log.addHandler(fh)

        log.addHandler(ch)
        return log 
Example 5
Project: pyblish-win   Author: pyblish   File: test_logging.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def test_encoding_plain_file(self):
        # In Python 2.x, a plain file object is treated as having no encoding.
        log = logging.getLogger("test")
        fn = tempfile.mktemp(".log")
        # the non-ascii data we write to the log.
        data = "foo\x80"
        try:
            handler = logging.FileHandler(fn)
            log.addHandler(handler)
            try:
                # write non-ascii data to the log.
                log.warning(data)
            finally:
                log.removeHandler(handler)
                handler.close()
            # check we wrote exactly those bytes, ignoring trailing \n etc
            f = open(fn)
            try:
                self.assertEqual(f.read().rstrip(), data)
            finally:
                f.close()
        finally:
            if os.path.isfile(fn):
                os.remove(fn) 
Example 6
Project: RelayBot2.0   Author: nukeop   File: logs.py    GNU General Public License v3.0 6 votes vote down vote up
def setup_logger(self, steamid, friend=False):
        name = (
            self.make_filename(
                self.bot.user.get_name_from_steamid(steamid),
                steamid
            ) if friend else
            self.make_filename(
                self.bot.user.groups.get_name(steamid),
                steamid
            )
        )
        self.loggers[steamid] = logging.getLogger(__name__+'.'+str(steamid))
        self.loggers[steamid].setLevel(logging.DEBUG)
        self.loggers[steamid].propagate = False
        formatter = logging.Formatter("%(asctime)s - %(message)s")
        filename = (os.path.join(self.friend_logs_path, name) if friend else
        os.path.join(self.group_logs_path, name))
        handler = logging.FileHandler(filename)
        handler.setFormatter(formatter)
        handler.setLevel(logging.DEBUG)
        self.loggers[steamid].addHandler(handler) 
Example 7
Project: iSDX   Author: sdn-ixp   File: logServer.py    Apache License 2.0 6 votes vote down vote up
def getLogger(fname=None):
    format='%(asctime)s:%(process)d:%(threadName)s:%(levelname)s:%(name)s:%(pathname)s %(lineno)d:%(message)s'
    formatter = MyFormatter(format)

    logger = logging.getLogger('sdx')

    if fname:
        fh = logging.FileHandler(fname)
        fh.setFormatter(formatter)
        logger.addHandler(fh)

    ch = logging.StreamHandler(stream=sys.stdout)
    ch.setFormatter(formatter)
    logger.addHandler(ch)

    return logger 
Example 8
Project: mmdetection   Author: open-mmlab   File: train.py    Apache License 2.0 6 votes vote down vote up
def get_root_logger(log_file=None, log_level=logging.INFO):
    logger = logging.getLogger('mmdet')
    # if the logger has been initialized, just return it
    if logger.hasHandlers():
        return logger

    logging.basicConfig(
        format='%(asctime)s - %(levelname)s - %(message)s', level=log_level)
    rank, _ = get_dist_info()
    if rank != 0:
        logger.setLevel('ERROR')
    elif log_file is not None:
        file_handler = logging.FileHandler(log_file, 'w')
        file_handler.setFormatter(
            logging.Formatter('%(asctime)s - %(levelname)s - %(message)s'))
        file_handler.setLevel(log_level)
        logger.addHandler(file_handler)

    return logger 
Example 9
Project: py_messenger   Author: cody-rocker   File: tools.py    GNU General Public License v2.0 6 votes vote down vote up
def init_logger(args):
    log = logging.getLogger('__name__')
    handler = None

    if (args.log_file_path is not None):
        handler = logging.FileHandler(
            args.log_file_path, 'w', encoding=None, delay='true')
    else:
        handler = logging.StreamHandler()

    # ref: https://docs.python.org/2/library/logging.html#logrecord-attributes
    log_format = ' \033[1;37m>>\033[0m \033[93m[%(funcName)s][%(levelname)s] \033[0;37m::\033[0m %(message)s'  # colored output

    handler.setFormatter(logging.Formatter(log_format))
    log.addHandler(handler)
    log.setLevel(getattr(logging, args.logging_level))
    return log 
Example 10
Project: mlimages   Author: icoxfog417   File: log_api.py    MIT License 6 votes vote down vote up
def create_file_logger(root, name, file_name="log.txt", timestamp_format="", debug=False):
    file_api = FileAPI(root)
    timestamp = ""
    if timestamp_format:
        timestamp = datetime.now().strftime(timestamp_format)
    else:
        timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")

    folder = name + "_" + timestamp
    # prepare folder and file
    with file_api.open_with_mkdir(folder + "/" + file_name) as f:
        f.write("".encode("utf-8"))

    log_root = os.path.join(root, folder)
    logger = create_logger(name, debug)
    fh = FileHandler(os.path.join(log_root, file_name), encoding="utf-8")
    fh.setLevel(_bool_2_level(debug))
    logger.addHandler(fh)

    # add close method to release resource
    logger.close = types.MethodType(__close, logger)
    return logger, log_root 
Example 11
Project: oslodatascience-rl   Author: Froskekongen   File: common.py    MIT License 6 votes vote down vote up
def __init__(self, logfile, console=False, level=logging.INFO, name=None):
        self.logfile = logfile
        self.level = level
        if name is None: name = __name__
        self.logger = logging.getLogger(name)
        self.logger.setLevel(level)
        
        formatter = logging.Formatter('%(asctime)s;%(message)s')
        self.fh = logging.FileHandler(logfile)
        self.fh.setFormatter(formatter)
        self.logger.addHandler(self.fh)

        if console:
            self.ch = logging.StreamHandler()
            self.ch.setFormatter(formatter)
            self.logger.addHandler(self.ch) 
Example 12
Project: django-qcloud-cos   Author: jasonham   File: utils.py    Apache License 2.0 6 votes vote down vote up
def get_logger(name='common', logfile=None):
    '''
    参数: name (str): logger name
        logfile (str): log file, 没有时使用stream handler
    返回:
        logger obj
    '''
    my_logger = logging.getLogger(name)
    my_logger.setLevel(logging.INFO)
    if logfile:
        handler = logging.FileHandler(logfile)
    else:
        handler = logging.StreamHandler()
    fmt = '%(asctime)s - %(name)s - %(levelname)s - %(filename)s - %(funcName)s - %(lineno)s - %(message)s'
    formatter = logging.Formatter(fmt)
    handler.setFormatter(formatter)
    my_logger.addHandler(handler)
    # 阻止冒泡
    my_logger.propagate = False
    return my_logger 
Example 13
Project: flasky   Author: RoseOu   File: glogging.py    MIT License 6 votes vote down vote up
def _set_handler(self, log, output, fmt):
        # remove previous gunicorn log handler
        h = self._get_gunicorn_handler(log)
        if h:
            log.handlers.remove(h)

        if output is not None:
            if output == "-":
                h = logging.StreamHandler()
            else:
                util.check_is_writeable(output)
                h = logging.FileHandler(output)
                # make sure the user can reopen the file
                try:
                    os.chown(h.baseFilename, self.cfg.user, self.cfg.group)
                except OSError:
                    # it's probably OK there, we assume the user has given
                    # /dev/null as a parameter.
                    pass

            h.setFormatter(fmt)
            h._gunicorn = True
            log.addHandler(h) 
Example 14
Project: PO_2_MLSA   Author: jvollme   File: PO_2_MLSA.py    GNU General Public License v3.0 6 votes vote down vote up
def setlogger(logfilename): #shall replace datsanerror, datsanlogmessage  datswarning
	#format logger
	logFormatter = logging.Formatter("[%(levelname)s]  %(message)s")
	mylogger = logging.getLogger()
	if args.debug:
		mylogger.setLevel(logging.DEBUG)
	else:
		mylogger.setLevel(logging.INFO)
		
	#set logger to write to logfile
	fileHandler = logging.FileHandler(logfilename)
	fileHandler.setFormatter(logFormatter)
	mylogger.addHandler(fileHandler)
	
	#set loger to write to stderr also
	consoleHandler = logging.StreamHandler()
	consoleHandler.setFormatter(logFormatter)
	if args.no_verbose:
		consoleHandler.setlevel(logging.WARNING)
	mylogger.addHandler(consoleHandler)
	
	return mylogger 
Example 15
Project: PO_2_MLSA   Author: jvollme   File: PO_2_MLSA_with_partials.py    GNU General Public License v3.0 6 votes vote down vote up
def setlogger(logfilename): #shall replace datsanerror, datsanlogmessage  datswarning
	#format logger
	logFormatter = logging.Formatter("[%(levelname)s]  %(message)s")
	mylogger = logging.getLogger()
	if args.debug:
		mylogger.setLevel(logging.DEBUG)
	else:
		mylogger.setLevel(logging.INFO)
		
	#set logger to write to logfile
	fileHandler = logging.FileHandler(logfilename)
	fileHandler.setFormatter(logFormatter)
	mylogger.addHandler(fileHandler)
	
	#set loger to write to stderr also
	consoleHandler = logging.StreamHandler()
	consoleHandler.setFormatter(logFormatter)
	if args.no_verbose:
		consoleHandler.setlevel(logging.WARNING)
	mylogger.addHandler(consoleHandler)
	
	return mylogger 
Example 16
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: benchmark.py    Apache License 2.0 6 votes vote down vote up
def setup_logging(log_loc):
    if os.path.exists(log_loc):
        shutil.move(log_loc, log_loc + "_" + str(int(os.path.getctime(log_loc))))
    os.makedirs(log_loc)

    log_file = '{}/benchmark.log'.format(log_loc)
    LOGGER = logging.getLogger('benchmark')
    LOGGER.setLevel(logging.INFO)
    formatter = logging.Formatter('%(asctime)s %(levelname)s:%(name)s %(message)s')
    file_handler = logging.FileHandler(log_file)
    console_handler = logging.StreamHandler()
    file_handler.setFormatter(formatter)
    console_handler.setFormatter(formatter)

    LOGGER.addHandler(file_handler)
    LOGGER.addHandler(console_handler)
    return LOGGER 
Example 17
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: common.py    Apache License 2.0 6 votes vote down vote up
def create_logger():
    logger = logging.getLogger('sgd_svrg')
    logger.setLevel(logging.INFO)
    formatter = logging.Formatter('%(asctime)s - %(message)s')
    fh = logging.FileHandler('experiments.log')
    fh.setFormatter(formatter)
    logger.addHandler(fh)
    return logger


################################################################################
# Functions below are for benchmark purpose to calcuate expectation, variance of
# gradients per epoch for each parameter. These calculations will be helpful when
# benchmarking SVRG optimization with other optimization techniques, such as SGD.
# Currently it only calculates the expectation, variance for single context but
# can be extended to multi-context in later iterations.
################################################################################ 
Example 18
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: utils.py    Apache License 2.0 6 votes vote down vote up
def logging_config(name=None, level=logging.DEBUG, console_level=logging.DEBUG):
    if name is None:
        name = inspect.stack()[1][1].split('.')[0]
    folder = os.path.join(os.getcwd(), name)
    if not os.path.exists(folder):
        os.makedirs(folder)
    logpath = os.path.join(folder, name + ".log")
    print("All Logs will be saved to %s"  %logpath)
    logging.root.setLevel(level)
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    logfile = logging.FileHandler(logpath)
    logfile.setLevel(level)
    logfile.setFormatter(formatter)
    logging.root.addHandler(logfile)
    #TODO Update logging patterns in other files
    logconsole = logging.StreamHandler()
    logconsole.setLevel(console_level)
    logconsole.setFormatter(formatter)
    logging.root.addHandler(logconsole)
    return folder 
Example 19
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: a3c.py    Apache License 2.0 6 votes vote down vote up
def log_config(log_dir=None, log_file=None, prefix=None, rank=0):
    reload(logging)
    head = '%(asctime)-15s Node[' + str(rank) + '] %(message)s'
    if log_dir:
        logging.basicConfig(level=logging.DEBUG, format=head)
        if not os.path.exists(log_dir):
            os.makedirs(log_dir)
        if not log_file:
            log_file = (prefix if prefix else '') + datetime.now().strftime('_%Y_%m_%d-%H_%M.log')
            log_file = log_file.replace('/', '-')
        else:
            log_file = log_file
        log_file_full_name = os.path.join(log_dir, log_file)
        handler = logging.FileHandler(log_file_full_name, mode='w')
        formatter = logging.Formatter(head)
        handler.setFormatter(formatter)
        logging.getLogger().addHandler(handler)
        logging.info('start with arguments %s', args)
    else:
        logging.basicConfig(level=logging.DEBUG, format=head)
        logging.info('start with arguments %s', args) 
Example 20
Project: GSM-SGD   Author: DingXiaoH   File: logger.py    MIT License 6 votes vote down vote up
def get_logger(name='', save_dir=None, distributed_rank=0, filename="log.txt"):
    logger = logging.getLogger(name)
    coloredlogs.install(level='DEBUG', logger=logger)
    # logger.setLevel(logging.DEBUG)
    # don't log results for the non-master process
    if distributed_rank > 0:
        return logger
    formatter = logging.Formatter(
        "%(asctime)s %(name)s %(levelname)s: %(message)s")

    # ch = logging.StreamHandler(stream=sys.stdout)
    # ch.setLevel(logging.DEBUG)
    # ch.setFormatter(formatter)
    # logger.addHandler(ch)

    if save_dir:
        fh = logging.FileHandler(os.path.join(save_dir, filename))
        fh.setLevel(logging.DEBUG)
        fh.setFormatter(formatter)
        if len(logger.handlers) > 0:
            logger.removeHandler(logger.handlers[0])
        logger.addHandler(fh)

    return logger 
Example 21
Project: BDCN   Author: wandoucao   File: log.py    MIT License 6 votes vote down vote up
def get_logger(f='log.txt', mode='w'):
    logger = logging.getLogger('bdcn')
    logger.setLevel(logging.DEBUG)
    formatter = logging.Formatter(
        '%(asctime)s - %(process)d - %(funcName)s - %(lineno)d: - %(message)s',
        datefmt='%Y-%m-%d %H:%M:%S')

    fh = logging.FileHandler(f, mode=mode)
    fh.setLevel(logging.DEBUG)
    fh.setFormatter(formatter)

    ch = logging.StreamHandler()
    ch.setLevel(logging.DEBUG)
    ch.setFormatter(formatter)

    logger.addHandler(ch)
    logger.addHandler(fh)
    return logger 
Example 22
Project: dogTorch   Author: ehsanik   File: main.py    MIT License 6 votes vote down vote up
def setup_logging(filepath, verbose):
    logFormatter = logging.Formatter(
        '%(levelname)s %(asctime)-20s:\t %(message)s')
    rootLogger = logging.getLogger()
    if verbose:
        rootLogger.setLevel(logging.DEBUG)
    else:
        rootLogger.setLevel(logging.INFO)
    logging.getLogger('PIL').setLevel(logging.WARNING)

    # Setup the logger to write into file
    fileHandler = logging.FileHandler(filepath)
    fileHandler.setFormatter(logFormatter)
    rootLogger.addHandler(fileHandler)

    # Setup the logger to write into stdout
    consoleHandler = logging.StreamHandler(sys.stdout)
    consoleHandler.setFormatter(logFormatter)
    rootLogger.addHandler(consoleHandler) 
Example 23
Project: cs294-112_hws   Author: xuwd11   File: main.py    MIT License 6 votes vote down vote up
def train(session, model, curr_dir, data_train, data_val):
    curr_dir = os.path.join(curr_dir, model.algorithm)
    bestmodel_dir = os.path.join(curr_dir, 'best_checkpoint')
    
    if not os.path.exists(curr_dir):
        os.makedirs(curr_dir)
    
    file_handler = logging.FileHandler(os.path.join(curr_dir, 'log.txt'))
    logging.getLogger().addHandler(file_handler)
    
    with open(os.path.join(curr_dir, FLAGS['save_name'] + '.json'), 'w') as f:
        json.dump(FLAGS, f)
    
    if not os.path.exists(bestmodel_dir):
        os.makedirs(bestmodel_dir)
    
    initialize_model(session, model, curr_dir, expect_exists=False)
    model.train(session, curr_dir, bestmodel_dir, data_train, data_val) 
Example 24
Project: GCN-SeA   Author: sumanbanerjee1   File: GCN_SeAN.py    Apache License 2.0 6 votes vote down vote up
def get_logger(log_dir, config_id):
    """
    Initialize Logger
    """
    if not os.path.exists(log_dir+config_id):
        os.makedirs(log_dir+config_id)
    logger = logging.getLogger()
    logger.setLevel(logging.DEBUG)
     
    handler1 = logging.StreamHandler(sys.stdout)
    handler1.setLevel(logging.INFO)
    formatter = logging.Formatter('%(asctime)s - [%(levelname)s] - %(message)s')
    handler1.setFormatter(formatter)
    logger.addHandler(handler1)
 
    handler2 = logging.FileHandler(os.path.join(log_dir+config_id, "logs.log"),"w", encoding=None, delay="true")
    handler2.setLevel(logging.INFO)
    formatter = logging.Formatter('%(asctime)s - [%(levelname)s] - %(message)s')
    handler2.setFormatter(formatter)
    logger.addHandler(handler2)
    return logger 
Example 25
Project: douyin   Author: luocaiwei   File: log.py    BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def _get_handlers(cls):
        enabled = config.LOG_ENABLE
        handlers = []
        if enabled:
            _level = config.LOG_LEVEL
            f_path = config.LOG_FILE_SAVE_PATH
            encoding = config.LOG_FILE_ENCODING
            _formatter = config.LOG_FORMAT[_level]
            _datefmt = config.LOG_DATE_FORMAT
            level = getattr(logging,_level)
            formatter = logging.Formatter(_formatter,datefmt=_datefmt)
            if f_path:
                encoding = encoding if encoding else cls.encoding
                f_handler = logging.FileHandler(f_path,
                                                encoding=encoding)
                f_handler.setLevel(logging.DEBUG)
                f_handler.setFormatter(formatter)
                handlers.append(f_handler)
            s_handler = logging.StreamHandler(stream=sys.stdout)
            s_handler.setLevel(level)
            s_handler.setFormatter(formatter)
            handlers.append(s_handler)
        else:
            handlers.append(logging.NullHandler())
        return handlers 
Example 26
Project: web-scraper   Author: keeper-of-data   File: log.py    MIT License 6 votes vote down vote up
def setup_custom_logger(name, log_file):

    # Create logger
    logger = logging.getLogger(name)
    logger.setLevel(logging.DEBUG)
    # Create file handler which logs error messages
    fh = logging.FileHandler(log_file)
    fh.setLevel(logging.ERROR)
    # Create console handler with a higher log level
    ch = logging.StreamHandler()
    ch.setLevel(logging.CRITICAL)
    # Create formatter and add it to the handlers
    formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(module)s - %(message)s')
    fh.setFormatter(formatter)
    ch.setFormatter(formatter)
    # Add the handlers to the logger
    logger.addHandler(fh)
    logger.addHandler(ch)

    return logger 
Example 27
Project: PYMusicPlayer   Author: wangzhenjjcn   File: main.py    GNU General Public License v3.0 6 votes vote down vote up
def set_logger():
    logger = logging.getLogger()
    logger.setLevel(LOG_LEVEL)
    formatter = logging.Formatter(fmt=LOG_FORMAT, datefmt='%Y-%m-%d %H:%M:%S')

    ch = logging.StreamHandler()
    ch.setLevel(logging.DEBUG)
    ch.setFormatter(formatter)
    logger.addHandler(ch)

    if LOG_FILE:
        fh = logging.FileHandler(LOG_FILE, encoding="utf-8")
        fh.setLevel(LOG_LEVEL)
        fh.setFormatter(formatter)
        logger.addHandler(fh)

    return logger 
Example 28
Project: PYMusicPlayer   Author: wangzhenjjcn   File: Player.py    GNU General Public License v3.0 6 votes vote down vote up
def set_logger():
    logger = logging.getLogger()
    logger.setLevel(LOG_LEVEL)
    formatter = logging.Formatter(fmt=LOG_FORMAT, datefmt='%Y-%m-%d %H:%M:%S')

    ch = logging.StreamHandler()
    ch.setLevel(logging.DEBUG)
    ch.setFormatter(formatter)
    logger.addHandler(ch)

    if LOG_FILE:
        fh = logging.FileHandler(LOG_FILE, encoding="utf-8")
        fh.setLevel(LOG_LEVEL)
        fh.setFormatter(formatter)
        logger.addHandler(fh)

    return logger 
Example 29
Project: openhatch   Author: campbe13   File: compat.py    GNU Affero General Public License v3.0 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 30
Project: medicaldetectiontoolkit   Author: MIC-DKFZ   File: exp_utils.py    Apache License 2.0 6 votes vote down vote up
def get_logger(exp_dir):
    """
    creates logger instance. writing out info to file and to terminal.
    :param exp_dir: experiment directory, where exec.log file is stored.
    :return: logger instance.
    """

    logger = logging.getLogger('medicaldetectiontoolkit')
    logger.setLevel(logging.DEBUG)
    log_file = exp_dir + '/exec.log'
    hdlr = logging.FileHandler(log_file)
    print('Logging to {}'.format(log_file))
    logger.addHandler(hdlr)
    logger.addHandler(ColorHandler())
    logger.propagate = False
    return logger 
Example 31
Project: FormulaNet   Author: princeton-vl   File: log.py    BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def get_logger(name=None, args=None, level=INFO, append=False):
    """Get customized logger.

    Args:
        name: Name of the logger.
        level: Level to log.

    Returns:
        A logger.
    """
    logger = logging.getLogger(name)
    if name and not getattr(logger, '_init_done', None):
        logger._init_done = True
        logger.addHandler(_handler)
        if args is not None:
            fileHandler = None
            if append:
                fileHandler = logging.FileHandler(args.log, mode='a')
            else:
                fileHandler = logging.FileHandler(args.log, mode='w')
            fileHandler.setFormatter(_Formatter())
            logger.addHandler(fileHandler)
        logger.setLevel(level)
    return logger 
Example 32
Project: redditswapbot   Author: thelectronicnub   File: log_conf.py    GNU General Public License v3.0 6 votes vote down vote up
def getLogger(name=None):
		#configure logging
		LoggerManager._loggers[name] = logging.getLogger(name)
		LoggerManager._loggers[name].setLevel(logging.INFO)
		
		if logging_dest == 'mysql':
			db = {'host':mysql_hostname, 'port':3306, 'dbuser':mysql_username, 'dbpassword':mysql_password, 'dbname':mysql_database}
		
			sqlh = mySQLHandler.mySQLHandler(db)
			LoggerManager._loggers[name].addHandler(sqlh)
		else:
			fileh = logging.FileHandler('actions.log')
			fileh.setFormatter(logging.Formatter('%(asctime)s - %(module)s - %(message)s'))
			LoggerManager._loggers[name].addHandler(fileh)
		
		requests_log = logging.getLogger("requests")
		requests_log.setLevel(logging.WARNING)
		
		return LoggerManager._loggers[name] 
Example 33
Project: microcache   Author: ajk8   File: __init__.py    MIT License 6 votes vote down vote up
def init_logging(stream=sys.stderr, filepath=None,
                 format='%(asctime).19s [%(levelname)s] %(name)s: %(message)s'):
    """
    Setup logging for the microcache module, but only do it once!

    :param stream: stream to log to (defaults to sys.stderr)
    :param filepath: path to a file to log to as well (defaults to None)
    :param format: override the default format with whatever you like
    """
    if not (len(logger.handlers) == 1 and isinstance(logger.handlers[0], logging.NullHandler)):
        logger.warn('logging has already been initialized, refusing to do it again')
        return
    formatter = logging.Formatter(format)
    if stream is not None:
        handler = logging.StreamHandler(stream=stream)
        handler.setFormatter(formatter)
        logger.addHandler(handler)
    if filepath is not None:
        handler = logging.FileHandler(filename=filepath)
        handler.setFormatter(formatter)
        logger.addHandler(handler)
    logger.info('successfully initialized logger') 
Example 34
Project: pyblish-win   Author: pyblish   File: handlers.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def __init__(self, filename, mode, encoding=None, delay=0):
        """
        Use the specified filename for streamed logging
        """
        if codecs is None:
            encoding = None
        logging.FileHandler.__init__(self, filename, mode, encoding, delay)
        self.mode = mode
        self.encoding = encoding 
Example 35
Project: pyblish-win   Author: pyblish   File: handlers.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def emit(self, record):
        """
        Emit a record.

        Output the record to the file, catering for rollover as described
        in doRollover().
        """
        try:
            if self.shouldRollover(record):
                self.doRollover()
            logging.FileHandler.emit(self, record)
        except (KeyboardInterrupt, SystemExit):
            raise
        except:
            self.handleError(record) 
Example 36
Project: pyblish-win   Author: pyblish   File: handlers.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def __init__(self, filename, mode='a', encoding=None, delay=0):
        logging.FileHandler.__init__(self, filename, mode, encoding, delay)
        self.dev, self.ino = -1, -1
        self._statstream() 
Example 37
Project: pyblish-win   Author: pyblish   File: handlers.py    GNU Lesser General Public License v3.0 5 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.
        """
        # Reduce the chance of race conditions by stat'ing by path only
        # once and then fstat'ing our new fd if we opened a new log stream.
        # See issue #14632: Thanks to John Mulligan for the problem report
        # and patch.
        try:
            # stat the file by path, checking for existence
            sres = os.stat(self.baseFilename)
        except OSError as err:
            if err.errno == errno.ENOENT:
                sres = None
            else:
                raise
        # compare file system stat with that of our stream file handle
        if not sres or sres[ST_DEV] != self.dev or sres[ST_INO] != self.ino:
            if self.stream is not None:
                # we have an open file handle, clean it up
                self.stream.flush()
                self.stream.close()
                self.stream = None  # See Issue #21742: _open () might fail.
                # open a new file handle and get new stat info from that fd
                self.stream = self._open()
                self._statstream()
        logging.FileHandler.emit(self, record) 
Example 38
Project: dnsbin   Author: thomas-maurice   File: logger.py    Do What The F*ck You Want To Public License 5 votes vote down vote up
def setup_logger(name, to_stdout=True, file_name=None):
    """Creates the logging object used by the script

    By defaults it prints information ton stdout, but
    you can tell it to print out information ton a file too
    """
    logger = logging.getLogger(name)
    logger.setLevel(logging.DEBUG)
    formatter = logging.Formatter(
        '%(asctime)s %(levelname)s: %(message)s'
    )

    # reset handlers
    for handler in logger.handlers:
        # Don't close stdout or stderr !
        if handler.__class__ != logging.StreamHandler:
            handler.stream.close()
        logger.removeHandler(handler)

    if file_name:
        fhandle = logging.FileHandler(file_name)
        fhandle.setLevel(logging.DEBUG)
        fhandle.setFormatter(formatter)
        logger.addHandler(fhandle)

    if to_stdout:
        chandle = logging.StreamHandler()
        chandle.setLevel(logging.DEBUG)
        chandle.setFormatter(formatter)
        logger.addHandler(chandle)

    return logger 
Example 39
Project: auto-check-in   Author: zeekvfu   File: utility.py    GNU General Public License v3.0 5 votes vote down vote up
def get_logger(log_file, log_level=logging.DEBUG):
    _format = '%(asctime)s %(process)d %(thread)d %(levelname)s | %(message)s'
    formatter = logging.Formatter(_format)

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

    logger = logging.getLogger(log_file)
    logger.addHandler(file_handler)
    if isinstance(log_level, str):
        log_level = eval(log_level)
    logger.setLevel(log_level)
    return logger 
Example 40
Project: utilities   Author: czbiohub   File: log_util.py    MIT License 5 votes vote down vote up
def get_logger(name, debug=False, dryrun=False):
    logger = logging.getLogger(name)
    logger.setLevel(logging.DEBUG)

    # create a logging format
    if dryrun:
        formatter = logging.Formatter(
            "%(asctime)s - %(name)s - %(levelname)s - (DRYRUN) - %(message)s"
        )
    else:
        formatter = logging.Formatter(
            "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
        )

    stream_handler = logging.StreamHandler()
    stream_handler.setLevel(logging.DEBUG if debug else logging.INFO)
    stream_handler.setFormatter(formatter)

    logger.addHandler(stream_handler)

    if os.environ.get("AWS_BATCH_JOB_ID"):
        log_file = os.path.abspath("{}.log".format(os.environ["AWS_BATCH_JOB_ID"]))
        file_handler = logging.FileHandler(log_file)
        file_handler.setLevel(logging.DEBUG)
        file_handler.setFormatter(formatter)

        # add the handlers to the logger
        logger.addHandler(file_handler)
    else:
        log_file = None
        file_handler = None

    return logger, log_file, file_handler 
Example 41
Project: MusicDownloader   Author: wwwpf   File: utils.py    GNU General Public License v3.0 5 votes vote down vote up
def init_logging():
    FILE_NAME = "log.txt"
    file_handler = logging.FileHandler(FILE_NAME, encoding="utf-8")
    FORMAT = "%(asctime)s %(filename)s[line:%(lineno)d]\t"\
        "%(levelname)s\t%(message)s"
    logging.basicConfig(handlers=[file_handler],
                        level=logging.INFO, format=FORMAT) 
Example 42
Project: esp-sdk-python   Author: EvidentSecurity   File: configuration.py    MIT License 5 votes vote down vote up
def logger_file(self, value):
        """
        Sets the logger_file.

        If the logger_file is None, then add stream handler and remove file handler.
        Otherwise, add file handler and remove stream handler.

        :param value: The logger_file path.
        :type: str
        """
        self.__logger_file = value
        if self.__logger_file:
            # If set logging file,
            # then add file handler and remove stream handler.
            self.logger_file_handler = logging.FileHandler(self.__logger_file)
            self.logger_file_handler.setFormatter(self.logger_formatter)
            for _, logger in iteritems(self.logger):
                logger.addHandler(self.logger_file_handler)
                if self.logger_stream_handler:
                    logger.removeHandler(self.logger_stream_handler)
        else:
            # If not set logging file,
            # then add stream handler and remove file handler.
            self.logger_stream_handler = logging.StreamHandler()
            self.logger_stream_handler.setFormatter(self.logger_formatter)
            for _, logger in iteritems(self.logger):
                logger.addHandler(self.logger_stream_handler)
                if self.logger_file_handler:
                    logger.removeHandler(self.logger_file_handler) 
Example 43
Project: flasky   Author: RoseOu   File: glogging.py    MIT License 5 votes vote down vote up
def reopen_files(self):
        for log in loggers():
            for handler in log.handlers:
                if isinstance(handler, logging.FileHandler):
                    handler.acquire()
                    try:
                        if handler.stream:
                            handler.stream.close()
                            handler.stream = open(handler.baseFilename,
                                    handler.mode)
                    finally:
                        handler.release() 
Example 44
Project: flasky   Author: RoseOu   File: glogging.py    MIT License 5 votes vote down vote up
def close_on_exec(self):
        for log in loggers():
            for handler in log.handlers:
                if isinstance(handler, logging.FileHandler):
                    handler.acquire()
                    try:
                        if handler.stream:
                            util.close_on_exec(handler.stream.fileno())
                    finally:
                        handler.release() 
Example 45
Project: Nurevam   Author: Maverun   File: events.py    MIT License 5 votes vote down vote up
def set_error(self,ctx,cog=None):
        """
        On a prod guild, it can get very spamming, so I would set it for just in case...
        """
        if cog:
            check = self.debug_cog.get(cog)
            if check or check is False:
                log = logging.getLogger("cogs.{}".format(cog))
                if check == True:
                    log.setLevel(logging.INFO)
                    utils.prPurple("Getting info to paste into hastebin")
                    with open("bot_log.txt","r+") as f:
                        msg = await utils.send_hastebin(f.read())
                        await ctx.send(content = msg)
                else:
                    log.setLevel(logging.DEBUG)
                self.debug_cog[cog] = not(check)
                await ctx.send("Set to {}".format(not(check)))
            else:
                log = logging.getLogger("cogs.{}".format(cog ))
                log.setLevel(logging.DEBUG)
                format_log = logging.Formatter('%(asctime)s:\t%(levelname)s:\t%(name)s:\tFunction:%(funcName)s ||| MSG: %(message)s')
                console = logging.StreamHandler()
                console.setFormatter(format_log)
                handler = logging.FileHandler(filename='bot_log.txt', encoding='utf-8', mode='w')
                handler.setFormatter(format_log)
                log.addHandler(console)
                log.addHandler(handler)
                self.debug_cog[cog] = True
                await ctx.send("Set to True")
        else:
            self.error_log = not (self.error_log)
            await ctx.send("Set {}".format(self.error_log)) 
Example 46
Project: o2g   Author: hiposfer   File: cli.py    MIT License 5 votes vote down vote up
def capture_logs():
    logfile = tempfile.mktemp()
    handler = FileHandler(logfile, mode='w')
    logging.getLogger().addHandler(handler)
    try:
        yield logfile
    finally:
        logging.getLogger().removeHandler(handler) 
Example 47
Project: healthbot-py-client   Author: Juniper   File: configuration.py    Apache License 2.0 5 votes vote down vote up
def logger_file(self, value):
        """The logger file.

        If the logger_file is None, then add stream handler and remove file
        handler. Otherwise, add file handler and remove stream handler.

        :param value: The logger_file path.
        :type: str
        """
        self.__logger_file = value
        if self.__logger_file:
            # If set logging file,
            # then add file handler and remove stream handler.
            self.logger_file_handler = logging.FileHandler(self.__logger_file)
            self.logger_file_handler.setFormatter(self.logger_formatter)
            for _, logger in six.iteritems(self.logger):
                logger.addHandler(self.logger_file_handler)
                if self.logger_stream_handler:
                    logger.removeHandler(self.logger_stream_handler)
        else:
            # If not set logging file,
            # then add stream handler and remove file handler.
            self.logger_stream_handler = logging.StreamHandler()
            self.logger_stream_handler.setFormatter(self.logger_formatter)
            for _, logger in six.iteritems(self.logger):
                logger.addHandler(self.logger_stream_handler)
                if self.logger_file_handler:
                    logger.removeHandler(self.logger_file_handler) 
Example 48
Project: healthbot-py-client   Author: Juniper   File: configuration.py    Apache License 2.0 5 votes vote down vote up
def logger_file(self, value):
        """The logger file.

        If the logger_file is None, then add stream handler and remove file
        handler. Otherwise, add file handler and remove stream handler.

        :param value: The logger_file path.
        :type: str
        """
        self.__logger_file = value
        if self.__logger_file:
            # If set logging file,
            # then add file handler and remove stream handler.
            self.logger_file_handler = logging.FileHandler(self.__logger_file)
            self.logger_file_handler.setFormatter(self.logger_formatter)
            for _, logger in six.iteritems(self.logger):
                logger.addHandler(self.logger_file_handler)
                if self.logger_stream_handler:
                    logger.removeHandler(self.logger_stream_handler)
        else:
            # If not set logging file,
            # then add stream handler and remove file handler.
            self.logger_stream_handler = logging.StreamHandler()
            self.logger_stream_handler.setFormatter(self.logger_formatter)
            for _, logger in six.iteritems(self.logger):
                logger.addHandler(self.logger_stream_handler)
                if self.logger_file_handler:
                    logger.removeHandler(self.logger_file_handler) 
Example 49
Project: python-client   Author: wavefrontHQ   File: configuration.py    Apache License 2.0 5 votes vote down vote up
def logger_file(self, value):
        """The logger file.

        If the logger_file is None, then add stream handler and remove file
        handler. Otherwise, add file handler and remove stream handler.

        :param value: The logger_file path.
        :type: str
        """
        self.__logger_file = value
        if self.__logger_file:
            # If set logging file,
            # then add file handler and remove stream handler.
            self.logger_file_handler = logging.FileHandler(self.__logger_file)
            self.logger_file_handler.setFormatter(self.logger_formatter)
            for _, logger in six.iteritems(self.logger):
                logger.addHandler(self.logger_file_handler)
                if self.logger_stream_handler:
                    logger.removeHandler(self.logger_stream_handler)
        else:
            # If not set logging file,
            # then add stream handler and remove file handler.
            self.logger_stream_handler = logging.StreamHandler()
            self.logger_stream_handler.setFormatter(self.logger_formatter)
            for _, logger in six.iteritems(self.logger):
                logger.addHandler(self.logger_stream_handler)
                if self.logger_file_handler:
                    logger.removeHandler(self.logger_file_handler) 
Example 50
Project: easylogging   Author: AI35   File: easylogging.py    GNU General Public License v3.0 5 votes vote down vote up
def run():
	Dir = os.path.join(os.path.dirname(os.path.abspath(logname.dname)), 'logs')
	logFile = 'Log %s.log' % (strftime("%Y-%m-%d %Hh-%Mm-%Ss", gmtime()))
	Dirfilelog = os.path.join(Dir, logFile)


	logger = logging.getLogger(__name__)
	run.logger = logger
	logging.disable(level)
	if level < 50:
		logger.propagate = True
	else:
		logger.propagate = False

	try:
		if '--logfile' in sys.argv:
			if not os.path.exists('logs'):
				os.makedirs('logs')
			handler = logging.FileHandler(Dirfilelog)
			handler.setLevel(logging.DEBUG)
			formatter = logging.Formatter(formated)
			handler.setFormatter(formatter)
			logger.addHandler(handler)
			logging.disable(prop)

	except Exception:
		pass

	try:
		if '--logging' in sys.argv:
			logging.disable(0)
			logger.propagate = True
	except Exception:
		pass