def __init__(self, stream=None): """ Construct this formatter. Provides colored output if the stream parameter is specified and is an acceptable TTY. We print hardwired escape sequences, which will probably break in some circumstances; for this unfortunate shortcoming, we apologize. """ # select and construct format string import curses format = None if stream and hasattr(stream, "isatty") and stream.isatty(): curses.setupterm() # FIXME do nice block formatting, increasing column sizes as necessary if curses.tigetnum("colors") > 2: format = "%s%%(asctime)s%s %%(message)s" % ( TTY_VerboseFormatter._NAME_COLOR, TTY_VerboseFormatter._COLOR_END, ) if format is None: format = "%(name)s - %(levelname)s - %(message)s" # initialize this formatter Formatter.__init__(self, format, TTY_VerboseFormatter._DATE_FORMAT)
def __init__(self, coloured=False): fmt = "%(asctime)s %(address)s %(levelname)s: %(message)s" datefmt = "%Y-%m-%d %H:%M:%S" self.linefmt = " " self._coloured = coloured Formatter.__init__(self, fmt, datefmt)
def onerror(self, e): """Send here Exception and traceback by Error channel """ f = Formatter() history = json.dumps(self.channel_history, indent=2, default=datahandler) data = (history, xml_escape(f.formatException(sys.exc_info())),) if EXCEPTION_FLAVOR == 'html': traceback = '<pre>%s\n\n%s</pre>' % data else: traceback = '%s\n%s' % data if DEBUG: err_message = {'channel': WS_CHANNELS['ERROR_CHID'], 'pkg': {'exception': repr(e), 'tb': traceback}} else: err_message = {'channel': WS_CHANNELS['ERROR_CHID'], 'pkg': {'exception': 'error 500', 'tb': 'an error occurred'}} log.error(format_exception(e, None), extra=self.channel_history) report_exception(e, extra=self.channel_history) if not self.ws.closed: try: self.ws.send(json.dumps(err_message, separators=(', ', ':'))) except WebSocketError as e: if is_ws_error_abnormal(e): log.error('WebSocket fault: %s' % e.message, extra=self.channel_history)
def __init__(self, pidlog=False): pidlog = "[%(process)s] " if pidlog else "" self._normal_fmt = ("%(asctime)s " + pidlog + "[%(levelname)s %(name)s] %(message)s") self._context_fmt = ("%(asctime)s " + pidlog + "[%(levelname)s " "%(name)s] [%(context)s] %(message)s") Formatter.__init__(self, self._normal_fmt)
def emit(self, record): if record.exc_info: # The log formatter will use the cached exc_text in place of the # exc_info Traceback object; since Traceback objects can't be # pickled, use this to pass over the formatted exception text # instead. fmt = Formatter() record.exc_info = fmt.formatException(record.exc_info) self.queue.put(record)
def __init__(self, stream, *args, **kwds): Formatter.__init__(self, *args, **kwds) # Create a mapping of levels to format string which is prepared to # contain the escape sequences. term = TerminalController(stream) self.color_fmt = {} for levelno, colstr in self.colordef: slist = [getattr(term, c) for c in colstr.split()] + ['%s', term.NORMAL] self.color_fmt[levelno] = ''.join(slist)
def __init__(self, formatter_for_level=dict(), default=Formatter()): """ Use the given formatter_for_level map of {level: formatter} to format messages. If for a given level, a formatter does not exist in the formatter_for_level dictionary, the default formatter specified by 'default' is used instead. """ Formatter.__init__(self) self.formatter_for_level = formatter_for_level self.default = default
def __init__(self, prefix='', only_from=None): """ .. todo:: WRITEME """ Formatter.__init__(self) self._info_fmt = prefix + "%(message)s" self._fmt = prefix + "%(levelname)s (%(name)s): %(message)s" self._only_from = only_from
def __init__(self,recordfields = [], datefmt=None, customjson=None): """__init__ overrides the default constructor to accept a formatter specific list of metadata fields Args: recordfields : A list of strings referring to metadata fields on the record object. It can be empty. The list of fields will be added to the JSON record created by the formatter. """ Formatter.__init__(self,None,datefmt) self.recordfields = recordfields self.customjson = customjson
def __init__(self, stream=None): """ Construct this formatter. """ # construct the format string format = "%(message)s" # initialize this formatter Formatter.__init__(self, format, TTY_ConciseFormatter._DATE_FORMAT)
def noodlesapp(env, start_response): """ :rtype : noodles.http.Response :param env: :param start_response: :return: :rtype: :raise: """ # Get request object if get_config('ENCODE_SEMICOLON_IN_REQUEST') is True: env['QUERY_STRING'] = re.sub('[;]', '%3b', env['QUERY_STRING']) request = Request(env) if "HTTP_X_FORWARDED_FOR" in env: x_forwarded_for = env["HTTP_X_FORWARDED_FOR"].split(',')[:1] if x_forwarded_for: request.remote_addr = x_forwarded_for[0] #print("Try to handle url_path '%s'" % request.path) # Get callable object with routine method to handle request producer = dispatcher.get_callable(request) if not producer: # May be here an error,raise exception raise Exception('Can\'t find callable for this url path') # Callable function must return Response object try: response = middlewares.run_chain(producer, request) if not hasattr(response, 'is_noodles_response'): response = producer() # Capture traceback here and send it if debug mode except Exception as e: f = Formatter() if EXCEPTION_FLAVOR=='html': traceback = '<pre>%s\n\n%s</pre>' \ % (json.dumps(env, indent=2, default=datahandler), xml_escape(f.formatException(sys.exc_info()))) else: traceback = '%s\n%s' \ % (json.dumps(env, indent=2, default=datahandler), f.formatException(sys.exc_info())) extra = {'request': request} log.error(format_exception(e, None), extra=extra) report_exception(e, extra=extra) if DEBUG: response = Error500(e, traceback) else: response = Error500() finally: middlewares.end_chain(lambda x: x, request) return response(env, start_response)
def __init__(self, tag=None, withpid=False, **kwargs): super(SysLogHandler, self).__init__(**kwargs) # 产生tag的formatter fmt = tag or "%(name)s" if withpid: fmt += "[%(process)d]" self.tag_formatter = Formatter(fmt)
def format (self, record): # Add the color codes to the record record.__dict__.update(escape_codes) # If we recognise the level name, # add the levels color as `log_color` if record.levelname in self.log_colors: color = self.log_colors[record.levelname] record.log_color = escape_codes[color] else: record.log_color = "" #NOTE: caller_name is very slow #record.__dict__['caller_name'] = caller_name(9) record.__dict__['abbr_levelname'] = record.levelname[0] record.__dict__['pathname2'] = '.'.join(record.pathname.split('/')[-2:]).rstrip('.py') # Format the message if version_info > (2, 7): message = super(ColoredFormatter, self).format(record) else: message = Formatter.format(self, record) # Add a reset code to the end of the message (if it wasn't explicitly added in format str) if self.reset and not message.endswith(escape_codes['reset']): message += escape_codes['reset'] return message
def format(self, record): record = deepcopy(record) if self.usesColor: color_end = '\033[00m' try: color_begin= { logging.DEBUG : '', logging.INFO : '\033[01;32m', logging.WARNING : '\033[01;33m', logging.ERROR : '\033[01;31m', logging.CRITICAL : '\033[01;31m', logging.FATAL : '\033[00;31m' }[record.levelno] except: color_begin = '' color_end = '' record.levelname = color_begin+'*'+color_end #record.levelno == logging.ERROR or if record.levelno == logging.CRITICAL or record.levelno == logging.FATAL: record.msg = color_begin+record.msg+color_end if self.useIdents: ident = len(inspect.stack()) if ident<self.minIdent: self.minIdent = ident record.msg=' '*(ident-self.minIdent)+record.msg return Formatter.format(self, record)
def format(self, record): try: msg = super(ANSIFormatter, self).format(record) except: # for python2.6 # Formatter is old-style class in python2.6 and type is classobj # another trick: http://stackoverflow.com/a/18392639/1276501 msg = Formatter.format(self, record) lvl2color = { "DEBUG": "blue", "INFO": "green", "WARNING": "yellow", "ERROR": "red", "CRITICAL": "bgred" } rln = record.levelname if rln in lvl2color: return "[{0}]: {1}".format( utils.color_msg(lvl2color[rln], rln), msg ) else: return msg
def format(self, record): if not getattr(record, 'session', None): record.session = '' record.sessionLS = ' %s' % record.session record.sessionRS = '%s ' % record.session else: record.sessionLS = ' %s' % record.session record.sessionRS = '%s ' % record.session if not getattr(record, 'classname', None): record.classname = '' if not getattr(record, 'method', None): record.method = record.funcName if not getattr(record, 'classAndmethod', None): if record.method and record.classname: record.classAndMethod = '%s::%s' % (record.classname, record.method) else: record.classAndMethod = '' created = localtime(record.created) record.date = strftime('%Y-%m-%d', created) record.time = strftime('%H:%M:%S', created) record.asctimeshort = '%s %s' % (record.date, record.time) #return Formatter.format(self, record) ## splits the record and formats every line msg = record.getMessage() res = [] for line in msg.split("\n"): record.msg, record.args = line, None res.append(Formatter.format(self, record)) return "\n".join(res)
def format(self, record): plain_result = Formatter.format(self, record) for severity_threshold, severity_color in self.SEVERITY_COLOR_THRESHOLDS: if record.levelno >= severity_threshold: color = severity_color else: break return colorize(plain_result, color=color)
class CustomSMTPHandler(SMTPHandler): def __init__(self, mailhost, fromaddr, toaddrs, subject): super(CustomSMTPHandler, self).__init__( mailhost, fromaddr, toaddrs, subject ) self.subject_formatter = Formatter(subject) def getSubject(self, record): # noqa: N802 return self.subject_formatter.format(record).replace('\n', '')
class QTextHandler(QtGui.QTextEdit): def __init__(self, parent=None): super(QTextHandler, self).__init__(parent) def setFormatter(self, fmt=None): self.formatter = Formatter(fmt) def handle(self, record): self.append(self.formatter.format(record))
def __init__ (self, format, datefmt=None, log_colors=default_log_colors, reset=True, style='%'): """ :Parameters: - format (str): The format string to use - datefmt (str): A format string for the date - log_colors (dict): A mapping of log level names to color names - reset (bool): Implictly appends a reset code to all records unless set to False - style ('%' or '{' or '$'): The format style to use. No meaning prior to Python 3.2. The ``format``, ``datefmt`` and ``style`` args are passed on to the Formatter constructor. """ if version_info > (3, 2): super(ColoredFormatter, self).__init__(format, datefmt, style=style) elif version_info > (2, 7): super(ColoredFormatter, self).__init__(format, datefmt) else: Formatter.__init__(self, format, datefmt) self.log_colors = log_colors self.reset = reset
def format(self, record): levelname = record.levelname msg = Formatter.format(self, record) if levelname in COLORS: if stdout.isatty(): msg = COLORS[levelname] % msg else: print ("*" * 100, levelname, "(%s)" % type(levelname), "not in", COLORS.keys()) return msg
def format(self, record): rv = [] length = 0 for piece in _ws_re.split(Formatter.format(self, record)): length += len(piece) if length > 140: if length - len(piece) < 140: rv.append(u'…') break rv.append(piece) return u''.join(rv)
def format(self, record): try: msg = super(NonANSIFormatter, self).format(record) except: # for python2.6 # Formatter is old-style class in python2.6 and type is classobj # another trick: http://stackoverflow.com/a/18392639/1276501 msg = Formatter.format(self, record) rln = record.levelname return "[{0}]: {1}".format(rln, msg)
def format(self, record): """Format the record with colors.""" color = self.color_seq % (30 + self.colors[record.levelname]) message = Formatter.format(self, record) message = message.replace('$RESET', self.reset_seq)\ .replace('$BOLD', self.bold_seq)\ .replace('$COLOR', color) for color, value in self.colors.items(): message = message.replace( '$' + color, self.color_seq % (value + 30))\ .replace('$BG' + color, self.color_seq % (value + 40))\ .replace('$BG-' + color, self.color_seq % (value + 40)) return message + self.reset_seq
class SysLogHandler(_SysLogHandler): """ 扩展标准库的SysLogHandler,使用loggername作为tag """ def __init__(self, tag=None, withpid=False, **kwargs): super(SysLogHandler, self).__init__(**kwargs) # 产生tag的formatter fmt = tag or "%(name)s" if withpid: fmt += "[%(process)d]" self.tag_formatter = Formatter(fmt) def format(self, record): msg = super(SysLogHandler, self).format(record) tag = self.tag_formatter.format(record) return "%s: %s" % (tag, msg)
def format(self, record): colored_record = copy(record) # Add colors to levelname levelname = colored_record.levelname color = MAPPING.get(levelname, 'white') colored_record.levelname = COLORIZE(levelname, color) # Add colors to tagged message text msg = colored_record.getMessage() plain_msg, color_msg = self.color_words(msg) record.msg = plain_msg colored_record.msg = color_msg return Formatter.format(self, colored_record)
def format(self, record): try: message = super(SecureFormatter, self).format(record) except TypeError: # In pyhton 2.6 the Formatter does not seem to # be defined as # class Formatter(object) # Using it in the super-statement this will raise a TypeError message = Formatter.format(self, record) secured = False s = "" for c in message: if c in string.printable: s += c else: s += '.' secured = True if secured: s = "!!!Log Entry Secured by SecureFormatter!!! " + s return s
def format (self, record): # Add the color codes to the record record.__dict__.update(escape_codes) # If we recognise the level name, # add the levels color as `log_color` if record.levelname in self.log_colors: color = self.log_colors[record.levelname] record.log_color = escape_codes[color] else: record.log_color = "" # Format the message if version_info > (2, 7): message = super(ColoredFormatter, self).format(record) else: message = Formatter.format(self, record) # Add a reset code to the end of the message (if it wasn't explicitly added in format str) if self.reset and not message.endswith(escape_codes['reset']): message += escape_codes['reset'] return message
class UserFormatter(Formatter): formatter = None def __init__(self, fmt=None, datefmt=None): self.formatter = Formatter(fmt, datefmt) def format(self, record): from auditor import AUDIT_SESSION_ID from common.middleware import threadlocal user = threadlocal.get_current_user() if user and user.is_authenticated(): username = user.username else: username = '******' request = threadlocal.get_current_request() if request and getattr(request, AUDIT_SESSION_ID): sessionid = getattr(request, AUDIT_SESSION_ID) else: sessionid = "None" setattr(record, 'username', username) setattr(record, 'sessionid', sessionid) return self.formatter.format(record)
def set_formatter(self, fmt): self.__formatter = Formatter(fmt)
def create_file_handler(fn): h = FileHandler(filename=fn) h.setFormatter(Formatter(get_console_format())) return h
def format(self, record): lines = iter(Formatter.format(self, record).splitlines()) prefix = lines.next() return '\n'.join(prefix + line for line in lines)
import signal import sys from threading import Event, Thread from time import sleep, time from .compat import StringIO, iteritems, on_win from .constants import NULL from .path import expand from .._vendor.auxlib.decorators import memoizemethod from .._vendor.auxlib.logz import NullHandler from .._vendor.auxlib.type_coercion import boolify from .._vendor.tqdm import tqdm log = getLogger(__name__) _FORMATTER = Formatter( "%(levelname)s %(name)s:%(funcName)s(%(lineno)d): %(message)s") def dashlist(iterable, indent=2): return ''.join('\n' + ' ' * indent + '- ' + str(x) for x in iterable) class ContextDecorator(object): """Base class for a context manager class (implementing __enter__() and __exit__()) that also makes it a decorator. """ # TODO: figure out how to improve this pattern so e.g. swallow_broken_pipe doesn't have to be instantiated # NOQA def __call__(self, f): @wraps(f)
from flaskext.bcrypt import Bcrypt from flask.ext.restful import Api from flask.ext.admin import Admin from flask.ext.mail import Mail from functools import wraps from flask.ext.sqlalchemy import SQLAlchemy import flask_wtf # Config and App setups app = Flask(__name__) app.config.from_object('config-default') app.debug = app.config.get('DEBUG') # Log handler functionality handler = RotatingFileHandler(app.config.get('LOG_FILE'), maxBytes=10000000, backupCount=100) handler.setFormatter(Formatter( '%(asctime)s %(levelname)s: %(message)s ' '[in %(pathname)s:%(lineno)d]' )) handler.setLevel(app.config.get('LOG_LEVEL')) app.logger.addHandler(handler) # CSRF Protection csrf_protect = flask_wtf.CsrfProtect(app) # Initalize DB object db = SQLAlchemy(app) # Initalize Bcrypt object for password hashing bcrypt = Bcrypt(app) # Initalize flask mail object for email notifications flask_mail = Mail(app)
from logging import INFO, getLogger, Formatter from logging import StreamHandler, FileHandler from discord import Status from discord import version_info as dpy_ver from discord.ext.commands import AutoShardedBot bot = AutoShardedBot( command_prefix=";" ) if __name__ == "__main__": logger = getLogger() logger.setLevel(INFO) fmt = Formatter("%(asctime)s [%(levelname)s]: %(message)s", "%Y-%m-%d %H:%M:%S") s_handle, f_handle = StreamHandler(), FileHandler("bot.log") s_handle.setFormatter(fmt), f_handle.setFormatter(fmt) logger.addHandler(s_handle), logger.addHandler(f_handle) logger.info( "Starting discord bot / using" f" py({ py_ver.major}.{ py_ver.minor}.{ py_ver.micro}), " f"dpy({dpy_ver.major}.{dpy_ver.minor}.{dpy_ver.micro})" ) try: with open("token.txt", mode="r") as tk: token = tk.readline() except FileNotFoundError: logger.critical("token undefined") exit(-1)
import random import time import urllib2 import urllib import cookielib import pprint as pp from logging import getLogger, Formatter, INFO, DEBUG from logging.handlers import SysLogHandler try: from cloghandler import ConcurrentRotatingFileHandler as loghandler_rotfile except ImportError: from logging.handlers import RotatingFileHandler as loghandler_rotfile yclog = getLogger('yc') yclog.setLevel(INFO) logfmt = Formatter('[%(asctime)s][%(levelname)s] %(message)s') logdir = '%s/tmp/log/yc' % os.environ['HOME'] logfile = '%s/yc.log' % logdir #TODO: dynamic path instead of ugly static path. if not os.path.isfile(logfile): if not os.path.isdir(logdir): try: os.mkdir(logdir, 0755) except OSError, errmsg: print "Failed to mkdir: %s, %s!" % (logdir, str(errmsg)) sys.exit(99) open(logfile, 'w').close() # Rotate after 20M, keep 20 old copies. loghandler = loghandler_rotfile(logfile, "a", 20 * 1024 * 1024, 20) loghandler.setFormatter(logfmt) yclog.addHandler(loghandler)
# GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. from __future__ import division, print_function, unicode_literals import os import sys import inspect import logging from logging import Formatter, StreamHandler from logging.handlers import RotatingFileHandler from .constants import CONFIG_DIR as _CONFIG_DIR ACCESS_FORMATTER_GEVENT = Formatter("%(message)s") ACCESS_FORMATTER_TORNADO = Formatter("[%(asctime)s] %(message)s") FORMATTER = Formatter( "[%(asctime)s] %(levelname)5s {%(name)s:%(lineno)d} %(message)s") DEFAULT_LOG_LEVEL = logging.INFO DEFAULT_LOG_FILE = os.path.join(_CONFIG_DIR, "calibre-web.log") DEFAULT_ACCESS_LOG = os.path.join(_CONFIG_DIR, "access.log") LOG_TO_STDERR = '/dev/stderr' LOG_TO_STDOUT = '/dev/stdout' logging.addLevelName(logging.WARNING, "WARN") logging.addLevelName(logging.CRITICAL, "CRIT") def get(name=None):
return render_template('pages/home.html') @app.errorhandler(404) def not_found_error(error): return render_template('errors/404.html'), 404 @app.errorhandler(500) def server_error(error): return render_template('errors/500.html'), 500 if not app.debug: file_handler = FileHandler('error.log') file_handler.setFormatter( Formatter('%(asctime)s %(levelname)s: %(message)s [in %(pathname)s:%(lineno)d]') ) app.logger.setLevel(logging.INFO) file_handler.setLevel(logging.INFO) app.logger.addHandler(file_handler) app.logger.info('errors') #----------------------------------------------------------------------------# # Launch. #----------------------------------------------------------------------------# # Default port: if __name__ == '__main__': app.run() # Or specify port manually:
cfg.MYSQL_USER, cfg.MYSQL_PASSWORD, cfg.MYSQL_HOST, cfg.MYSQL_DB) db = SQLAlchemy(app) from app.common import elastic as es es = es.Elastic() # initial log application if cfg.DEBUG: from logging import Formatter import logging file_handler = logging.FileHandler(cfg.LOG_FILE) if cfg.LOG_LEVEL not in [0, 10, 20, 30, 40, 50]: file_handler.setLevel(0) else: file_handler.setLevel(cfg.LOG_LEVEL) file_handler.setFormatter(Formatter(cfg.LOG_FORMAT)) app.logger.addHandler(file_handler) # initial a module from app.module_search import controllers # initial blueprint app.register_blueprint(controllers.search, url_prefix='/search') # Error Handler @app.errorhandler(404) def not_found(error): return make_response( jsonify({ 'error_code': http.client.NOT_FOUND,
def main(): """Main routine""" debug = False try: argparser = ArgumentParser(description=modules[__name__].__doc__) argparser.add_argument('device', nargs='?', default='ftdi:///?', help='serial port device name') argparser.add_argument('-x', '--hexdump', action='store_true', help='dump EEPROM content as ASCII') argparser.add_argument('-X', '--hexblock', type=int, help='dump EEPROM as indented hexa blocks') argparser.add_argument('-i', '--input', type=FileType('rt'), help='input ini file to load EEPROM content') argparser.add_argument('-l', '--load', default='all', choices=('all', 'raw', 'values'), help='section(s) to load from input file') argparser.add_argument('-o', '--output', type=FileType('wt'), help='output ini file to save EEPROM content') argparser.add_argument('-s', '--serial-number', help='set serial number') argparser.add_argument('-m', '--manufacturer', help='set manufacturer name') argparser.add_argument('-p', '--product', help='set product name') argparser.add_argument('-c', '--config', action='append', help='change/configure a property ' 'as key=value pair') argparser.add_argument('-e', '--erase', action='store_true', help='erase the whole EEPROM content') argparser.add_argument('-u', '--update', action='store_true', help='perform actual update, use w/ care') argparser.add_argument('-P', '--vidpid', action='append', help='specify a custom VID:PID device ID, ' 'may be repeated') argparser.add_argument('-V', '--virtual', type=FileType('r'), help='use a virtual device, specified as YaML') argparser.add_argument('-v', '--verbose', action='count', default=0, help='increase verbosity') argparser.add_argument('-d', '--debug', action='store_true', help='enable debug mode') args = argparser.parse_args() debug = args.debug if not args.device: argparser.error('Serial device not specified') loglevel = max(DEBUG, ERROR - (10 * args.verbose)) loglevel = min(ERROR, loglevel) if debug: formatter = Formatter( '%(asctime)s.%(msecs)03d %(name)-20s ' '%(message)s', '%H:%M:%S') else: formatter = Formatter('%(message)s') FtdiLogger.set_formatter(formatter) FtdiLogger.set_level(loglevel) FtdiLogger.log.addHandler(StreamHandler(stderr)) if args.virtual: #pylint: disable-msg=import-outside-toplevel from pyftdi.usbtools import UsbTools # Force PyUSB to use PyFtdi test framework for USB backends UsbTools.BACKENDS = ('pyftdi.tests.backend.usbvirt', ) # Ensure the virtual backend can be found and is loaded backend = UsbTools.find_backend() loader = backend.create_loader()() loader.load(args.virtual) try: add_custom_devices(Ftdi, args.vidpid) except ValueError as exc: argparser.error(str(exc)) eeprom = FtdiEeprom() eeprom.open(args.device) if args.erase: eeprom.erase() if args.input: eeprom.load_config(args.input, args.load) if args.serial_number: eeprom.set_serial_number(args.serial_number) if args.manufacturer: eeprom.set_manufacturer_name(args.manufacturer) if args.product: eeprom.set_product_name(args.product) for conf in args.config or []: if conf == '?': helpstr = ', '.join(sorted(eeprom.properties)) print( fill(helpstr, initial_indent=' ', subsequent_indent=' ')) exit(1) for sep in ':=': if sep in conf: name, value = conf.split(sep, 1) if not value: argparser.error('Configuration %s without value' % conf) helpio = StringIO() eeprom.set_property(name, value, helpio) helpstr = helpio.getvalue() if helpstr: print( fill(helpstr, initial_indent=' ', subsequent_indent=' ')) exit(1) break else: argparser.error('Missing name:value separator in %s' % conf) if args.hexdump: print(hexdump(eeprom.data)) if args.hexblock is not None: indent = ' ' * args.hexblock for pos in range(0, len(eeprom.data), 16): hexa = ' '.join( ['%02x' % x for x in eeprom.data[pos:pos + 16]]) print(indent, hexa, sep='') if args.update: if eeprom.commit(False): eeprom.reset_device() if args.verbose > 0: eeprom.dump_config() if args.output: eeprom.save_config(args.output) except (ImportError, IOError, NotImplementedError, ValueError) as exc: print('\nError: %s' % exc, file=stderr) if debug: print(format_exc(chain=False), file=stderr) exit(1) except KeyboardInterrupt: exit(2)
import sys from datetime import datetime from io import TextIOBase from logging import FileHandler, Formatter, Handler, StreamHandler from pathlib import Path from typing import Optional import colorama from .env_vars import dispatcher_env_vars, trial_env_vars handlers = {} log_format = '[%(asctime)s] %(levelname)s (%(name)s/%(threadName)s) %(message)s' time_format = '%Y-%m-%d %H:%M:%S' formatter = Formatter(log_format, time_format) def init_logger() -> None: """ This function will (and should only) get invoked on the first time of importing nni (no matter which submodule). It will try to detect the running environment and setup logger accordingly. The detection should work in most cases but for `nnictl` and `nni.experiment`. They will be identified as "standalone" mode and must configure the logger by themselves. """ colorama.init() if dispatcher_env_vars.SDK_PROCESS == 'dispatcher': _init_logger_dispatcher() return
import subprocess import configparser import requests from requests.packages.urllib3.exceptions import InsecureRequestWarning requests.packages.urllib3.disable_warnings(InsecureRequestWarning) from logging import getLogger, DEBUG, StreamHandler, Formatter from logging.handlers import SysLogHandler logger = getLogger(__name__) logger.setLevel(DEBUG) stream = StreamHandler() syslog = SysLogHandler(address="/dev/log") syslog.setFormatter(Formatter("portconfig: %(message)s")) logger.addHandler(stream) logger.addHandler(syslog) logger.propagate = False ipcmd = "/bin/ip" brcmd = "/sbin/bridge" # PortConfig Instance global portconfig portconfig = None from flask import Flask app = Flask(__name__)
""" NOTE: line98 : iSMART tool command work confirm line105 : erase count correct keywords line239 : checkFail() fail item keywords """ Desktop = join('C:' + environ['HOMEPATH'], 'Desktop') counter, kb = 0, Dispatch("WScript.Shell") require_debug = input('Enable debug log mode? (default:N) [y/N]: ') if require_debug in ['y', 'Y']: basicConfig(filename='AutoCheckScript_' + strftime('%Y%m%d_%H-%M-%S') + '.log', level=DEBUG) logger = getLogger(__name__) fhandler = StreamHandler() fhandler.setFormatter(Formatter()) logger.addHandler(fhandler) status = 1 else: status = 0 class MultiProcess(Thread): def __init__(self, action, delay): Thread.__init__(self) self.action = action self.delay = delay def run(self): ProcessAction(self.action, self.delay)
# directory exists. Otherwise, it will end up in root logging # facility (ie, event.log). from App.config import getConfiguration instancehome = getConfiguration().instancehome if instancehome is not None: log_directory = path.join(instancehome, 'log') if path.isdir(log_directory): from Signals import Signals from ZConfig.components.logger.loghandler import FileHandler log_file_handler = FileHandler(path.join(log_directory, 'ERP5SyncML.log')) # Default zope log format string borrowed from # ZConfig/components/logger/factory.xml, but without the extra "------" # line separating entries. log_file_handler.setFormatter(Formatter( "%(asctime)s %(levelname)s %(name)s %(message)s", "%Y-%m-%dT%H:%M:%S")) Signals.registerZopeSignals([log_file_handler]) syncml_logger.addHandler(log_file_handler) syncml_logger.propagate = 0 def checkAlertCommand(syncml_request): """ This parse the alert commands received and return a dictionnary mapping database to sync mode """ database_alert_list = [] # XXX To be moved on engine search = getSite().portal_categories.syncml_alert_code.searchFolder for alert in syncml_request.alert_list:
def formatTime(self, record, datefmt=None): # Old-style class in python < 2.7 string = Formatter.formatTime(self, record, datefmt).decode('utf-8') return unicode(string)
def createColoredFormatter(stream, format): if (sys.platform != 'win32') and stream.isatty(): return ColoredFormatter(format) else: return Formatter(format)
def formatTime(self, record, datefmt=None): return Formatter.formatTime( self, record, '%Y-%m-%d %H:%M:%S') + '.%04d' % (record.msecs * 10)
def create_console_handler(): console_handler = StreamHandler() console_handler.setFormatter(Formatter(get_console_format())) return console_handler
def format(self, record): levelname = record.levelname msg = Formatter.format(self, record) if levelname in COLORS: msg = COLORS[levelname] % msg return msg
def formatMessage(self, record): s = Formatter.formatMessage(self, record) name = record.name lvlno = record.levelno logger = getLogger(name) color = { DEBUG: 'none', CHAT: 'cyan', INFO: 'cyan', NOTE: 'cyan', WARNING: 'yellow', ERROR: 'red', CRITICAL: 'red', }[lvlno] if lvlno == DEBUG or getattr(record, 'no_marker', False): marker = '' elif logger.isEnabledFor(DEBUG): marker = { DEBUG: '', CHAT: 'chat', INFO: 'info', NOTE: 'note', WARNING: 'warn', ERROR: 'error', CRITICAL: 'crit', }[lvlno] marker = "%s:%s" % (marker, self.shorter_name(name)) elif logger.isEnabledFor(CHAT): marker = { DEBUG: '', CHAT: '[i:%s]', INFO: '[i:%s]', NOTE: '[i:%s]', WARNING: '[w:%s]', ERROR: '[!:%s]', CRITICAL: '[!:%s]', }[lvlno] marker = marker % self.shorter_name(name) else: marker = { DEBUG: '', CHAT: '[i]', INFO: '[i]', NOTE: '[i]', WARNING: '[w]', ERROR: '[!]', CRITICAL: '[!]', }[lvlno] use_colors = getLogger('byexample').use_colors if marker and not getattr(record, 'disable_prefix', False): marker = colored(marker, color, use_colors=use_colors) s = "%s %s" % (marker, s) ex = getattr(record, 'example', None) if ex is not None: if logger.isEnabledFor(DEBUG): ex.pretty_print() else: s += '\n' + indent(highlight_syntax(ex, use_colors)) return s
def main(): """Main routine""" debug = False try: argparser = ArgumentParser(description=modules[__name__].__doc__) argparser.add_argument('device', nargs='?', default='ftdi:///?', help='serial port device name') argparser.add_argument('-x', '--hexdump', action='store_true', help='dump EEPROM content as ASCII') argparser.add_argument('-o', '--output', type=FileType('wt'), help='output ini file to save EEPROM content') argparser.add_argument('-s', '--serial-number', help='set serial number') argparser.add_argument('-m', '--manufacturer', help='set manufacturer name') argparser.add_argument('-p', '--product', help='set product name') argparser.add_argument('-e', '--erase', action='store_true', help='erase the whole EEPROM content') argparser.add_argument('-u', '--update', action='store_true', help='perform actual update, use w/ care') argparser.add_argument('-v', '--verbose', action='count', default=0, help='increase verbosity') argparser.add_argument('-d', '--debug', action='store_true', help='enable debug mode') args = argparser.parse_args() debug = args.debug if not args.device: argparser.error('Serial device not specified') loglevel = max(DEBUG, ERROR - (10 * args.verbose)) loglevel = min(ERROR, loglevel) if debug: formatter = Formatter( '%(asctime)s.%(msecs)03d %(name)-20s ' '%(message)s', '%H:%M:%S') else: formatter = Formatter('%(message)s') FtdiLogger.set_formatter(formatter) FtdiLogger.set_level(loglevel) FtdiLogger.log.addHandler(StreamHandler(stderr)) eeprom = FtdiEeprom() eeprom.open(args.device) if args.erase: eeprom.erase() if args.serial_number: eeprom.set_serial_number(args.serial_number) if args.manufacturer: eeprom.set_manufacturer_name(args.manufacturer) if args.product: eeprom.set_product_name(args.product) if args.hexdump: print(hexdump(eeprom.data)) if args.update: eeprom.commit(False) if args.verbose > 0: eeprom.dump_config() if args.output: eeprom.save_config(args.output) except (IOError, ValueError) as exc: print('\nError: %s' % exc, file=stderr) if debug: print(format_exc(chain=False), file=stderr) exit(1) except KeyboardInterrupt: exit(2)
from sys import stdout from logging import getLogger, DEBUG, Formatter, StreamHandler logger = getLogger('itd-eroland-bot') logger.setLevel(DEBUG) logger_format = Formatter('%(asctime)s - %(levelname)s - %(message)s') console_handler = StreamHandler(stdout) console_handler.setFormatter(logger_format) logger.addHandler(console_handler)
def main(): """Entry point.""" debug = False try: argparser = ArgumentParser(description=modules[__name__].__doc__) argparser.add_argument('-P', '--vidpid', action='append', help='specify a custom VID:PID device ID, ' 'may be repeated') argparser.add_argument('-V', '--virtual', type=FileType('r'), help='use a virtual device, specified as YaML') argparser.add_argument('-v', '--verbose', action='count', default=0, help='increase verbosity') argparser.add_argument('-d', '--debug', action='store_true', help='enable debug mode') args = argparser.parse_args() debug = args.debug loglevel = max(DEBUG, ERROR - (10 * args.verbose)) loglevel = min(ERROR, loglevel) if debug: formatter = Formatter( '%(asctime)s.%(msecs)03d %(name)-20s ' '%(message)s', '%H:%M:%S') else: formatter = Formatter('%(message)s') FtdiLogger.set_formatter(formatter) FtdiLogger.set_level(loglevel) FtdiLogger.log.addHandler(StreamHandler(stderr)) if args.virtual: #pylint: disable-msg=import-outside-toplevel from pyftdi.usbtools import UsbTools # Force PyUSB to use PyFtdi test framework for USB backends UsbTools.BACKENDS = ('pyftdi.tests.backend.usbvirt', ) # Ensure the virtual backend can be found and is loaded backend = UsbTools.find_backend() loader = backend.create_loader()() loader.load(args.virtual) try: add_custom_devices(Ftdi, args.vidpid) except ValueError as exc: argparser.error(str(exc)) Ftdi.show_devices() except (ImportError, IOError, NotImplementedError, ValueError) as exc: print('\nError: %s' % exc, file=stderr) if debug: print(format_exc(chain=False), file=stderr) exit(1) except KeyboardInterrupt: exit(2)
def __init__(self, patern): Formatter.__init__(self, patern)
def __init__(self, methodName='runTest'): super().__init__(methodName) self.logger = getLogger() self.logger.setLevel(LogLevel.NOTSET) self.fmt: Formatter = Formatter("%(levelname)10s: [%(name)s] topic='%(topic)s' agent=%(agent)s context=%(context)s %(message)s") lastResort.setLevel(LogLevel.NOTSET)
def format(self, record): self._cur_record = record self._cur_logger = getLogger(record.name) return Formatter.format(self, record)
from logging import getLogger, Formatter, StreamHandler, FileHandler, DEBUG import pandas as pd from sklearn.model_selection import train_test_split from sklearn.ensemble import RandomForestRegressor # ログの出力名を設定 logger = getLogger(__name__) # ログのフォーマットを設定(詳細は次のマークダウンテーブル参考) fmt = Formatter('%(asctime)s %(name)s %(lineno)d %(levelname)s %(message)s') # ログのコンソール出力の設定 shandler = StreamHandler() shandler.setLevel('INFO') shandler.setFormatter(fmt) # ログのファイル出力先の設定 fhandler = FileHandler('result_tmp/RandomForestRegressor.log') fhandler.setLevel(DEBUG) fhandler.setFormatter(fmt) # ログレベルの設定 logger.setLevel(DEBUG) logger.addHandler(shandler) logger.addHandler(fhandler) logger.propagate = False logger.info('read_csv start') train = pd.read_csv('../../input/sales_train.csv') test = pd.read_csv('../../input/test.csv') submission = pd.read_csv('../../input/sample_submission.csv')
def setup_logger(log_filename): format_str = '%(asctime)s %(levelname)s %(message)s' basicConfig(filename=log_filename, level=DEBUG, format=format_str) stream_handler = StreamHandler() stream_handler.setFormatter(Formatter(format_str)) getLogger().addHandler(stream_handler)
def format(self, record): if record.args and hasattr(record, 'output_limit'): # Truncate all args to the set limit. record.args = tuple([truncate(arg, limit=record.output_limit) for arg in record.args]) return Formatter.format(self, record)
import pandas as pd import numpy as np # logging and loading libs from logging import StreamHandler, DEBUG, Formatter, FileHandler, getLogger from load_data import load_test_data, load_train_data DIR = 'result_tmp/' logger = getLogger(__name__) if __name__ == '__main__': log_fmt = Formatter( '%(asctime)s %(name)s %(lineno)d [%(levelname)s][%(funcName)s] %(message)s ' ) handler = StreamHandler() handler.setLevel('INFO') handler.setFormatter(log_fmt) logger.addHandler(handler) handler = FileHandler(DIR + 'ensemble.py.log', 'a') handler.setLevel(DEBUG) handler.setFormatter(log_fmt) logger.setLevel(DEBUG) logger.addHandler(handler) logger.info('start') # all results lg = pd.read_csv(DIR + 'submission_lg.csv') xgb = pd.read_csv(DIR + 'submission_xgb.csv') lgb = pd.read_csv(DIR + 'submission_lgb.csv')