def __init__(self, pipeline_id=None, host='127.0.0.1', port=50001): super(PipelineCallback, self).__init__() self._pipeline_id = pipeline_id if pipeline_id is not None else uuid4().hex self.logger = logging.getLogger(pipeline_id) handler = DatagramHandler(host, port) formatter = logging.Formatter(fmt="%(message)s") handler.setFormatter(formatter) handler.setLevel(logging.INFO) self.logger.addHandler(handler) self.logger.setLevel(logging.INFO)
def config_logging(suffix=""): from logging.handlers import TimedRotatingFileHandler, DatagramHandler base_format = logging.Formatter("%(asctime)s %(levelname)s %(message)s", "%Y-%m-%d %H:%M:%S") filename = "log-" + suffix + ".log" file_handler = TimedRotatingFileHandler(filename=filename, when='midnight', backupCount=15) file_handler.setFormatter(base_format) logging.getLogger().addHandler(file_handler) udp_handler = DatagramHandler(host="10.47.54.115", port=10000) # 设置 udp 服务器地址 udp_handler.setFormatter(base_format) udp_handler.setLevel(level=logging.ERROR) logging.getLogger().addHandler(udp_handler) logging.getLogger().setLevel(level=logging.INFO)
def log_init(log_path): _logger = logging.getLogger(logger_name) file_name = str(log_path).split('/')[-1].split('.')[0] datefmt = '%Y-%m-%d %I:%M:%S %p' fmt = '%(asctime)s-[%(levelname)s]-[' + file_name + ']: %(message)s' _logger.setLevel(logging.DEBUG) fh = logging.FileHandler(log_path) fh.setLevel(logging.INFO) fh.setFormatter(Formatter(fmt, datefmt)) _logger.addHandler(fh) ch = logging.StreamHandler(sys.stdout) ch.setLevel(logging.DEBUG) ch.setFormatter(MyFormatter(fmt, datefmt)) _logger.addHandler(ch) uh = DatagramHandler(const.const.TLOG_ADDR[0], const.const.TLOG_ADDR[1]) uh.setLevel(logging.CRITICAL) uh.setFormatter('%(message)s')
def log_init(log_path): _logger = logging.getLogger(logger_name) file_name = str(log_path).split('/')[-1].split('.')[0] datefmt = '%Y-%m-%d %I:%M:%S %p' fmt = '%(asctime)s-[%(levelname)s]-[' + file_name + ']: %(message)s' _logger.setLevel(logging.DEBUG) fh = logging.FileHandler(log_path) fh.setLevel(logging.ERROR) fh.setFormatter(Formatter(fmt, datefmt)) _logger.addHandler(fh) ch = logging.StreamHandler(sys.stdout) ch.setLevel(logging.DEBUG) ch.setFormatter(MyFormatter(fmt, datefmt)) _logger.addHandler(ch) uh = DatagramHandler(const.const.TLOG_ADDR[0], const.const.TLOG_ADDR[1]) uh.setLevel(logging.CRITICAL) uh.setFormatter('%(message)s')
# Console - all log messages with streamhandler console_handler = logging.StreamHandler() console_handler.setLevel(logging.DEBUG) console_handler.setFormatter(def_formatter) # Filehandler - warning and higher messages only, include current time # filename: {todays-date}.log # shares format with console/streamhandler file_handler = logging.FileHandler(f'{today_date}.log') file_handler.setLevel(logging.WARNING) file_handler.setFormatter(def_formatter) # SysLog Server - error and higher messages only, does not include current time sys_handler = DatagramHandler(host='127.0.0.1', port=514) sys_handler.setLevel(logging.ERROR) sys_handler.setFormatter(sys_formatter) logger = logging.getLogger() logger.setLevel(logging.DEBUG) logger.addHandler(file_handler) logger.addHandler(console_handler) logger.addHandler(sys_handler) def my_fun(n): for i in range(0, n): logging.debug(i) if i == 50: logging.warning("The value of i is 50.") try: i / (50 - i)
log_format_serv = "%(filename)s:%(lineno)-4d %(levelname)s %(message)s" formatter = logging.Formatter(log_format) formatter_serv = logging.Formatter(log_format_serv) file_handler = logging.FileHandler('{}.log'.format(datetime.date.today())) file_handler.setLevel(logging.WARNING) file_handler.setFormatter(formatter) console_handler = logging.StreamHandler() console_handler.setLevel(logging.DEBUG) console_handler.setFormatter(formatter) syslog_handler = DatagramHandler('127.0.0.1', 514) syslog_handler.setLevel(logging.ERROR) syslog_handler.setFormatter(formatter_serv) logger = logging.getLogger() logger.setLevel(logging.DEBUG) logger.addHandler(file_handler) logger.addHandler(console_handler) logger.addHandler(syslog_handler) def my_fun(n): for i in range(0, n): logging.debug(i) if i == 50: logging.warning("The value of i is 50.") try: 100 / (50 - i)
today = datetime.date.today() formatter = logging.Formatter(format) sysformatter = logging.Formatter(sysformat) file_handler = logging.FileHandler('{}.log'.format(today)) file_handler.setLevel(logging.WARNING) file_handler.setFormatter(formatter) console_handler = logging.StreamHandler() console_handler.setLevel(logging.DEBUG) console_handler.setFormatter(formatter) syslog_handler = DatagramHandler("127.0.0.1", 514) syslog_handler.setLevel(logging.ERROR) syslog_handler.setFormatter(sysformatter) logger = logging.getLogger() logger.setLevel(logging.DEBUG) logger.addHandler(file_handler) logger.addHandler(console_handler) logger.addHandler(syslog_handler) def my_fun(n): for i in range(0, n): logging.debug(i) if i == 50: logging.warning("The value of i is 50.") try: i / (50 - i)
format = "%(asctime)s %(filename)s:%(lineno)-3d %(levelname)s %(message)s" syslog_format = "%(filename)s:%(lineno)-3d %(levelname)s %(message)s" formatter = logging.Formatter(format) file_handler = logging.FileHandler(f"{date.today()}.log") file_handler.setLevel(logging.WARNING) file_handler.setFormatter(formatter) console_handler = logging.StreamHandler() console_handler.setLevel(logging.DEBUG) console_handler.setFormatter(formatter) syslog_server_handler = DatagramHandler(HOST, PORT) syslog_server_handler.setLevel(logging.ERROR) syslog_server_handler.setFormatter(syslog_format) logger = logging.getLogger() logger.setLevel(logging.DEBUG) logger.addHandler(file_handler) logger.addHandler(console_handler) logger.addHandler(syslog_server_handler) def my_fun(n): for i in range(0, n): logging.debug(i) if i == 50: logging.warning("The value of i is 50.") try: i / (50 - i)
def udpHandler(self): udp_handler = DatagramHandler('127.0.0.1', 3333) udp_handler.setLevel(self.log_level) udp_handler.setFormatter(self.formatter) self.logger.addHandler(udp_handler)
notime_format = "%(filename)s:%(lineno)-3d %(levelname)s %(message)s" formatter = logging.Formatter(format) notime_formatter = logging.Formatter(notime_format) file_handler = logging.FileHandler("{}.log".format(date.today())) file_handler.setLevel(logging.WARNING) file_handler.setFormatter(formatter) console_handler = logging.StreamHandler() console_handler.setLevel(logging.DEBUG) console_handler.setFormatter(formatter) syslog_server_handler = DatagramHandler(HOST, PORT) syslog_server_handler.setLevel(logging.ERROR) syslog_server_handler.setFormatter(notime_formatter) logger = logging.getLogger() logger.setLevel(logging.DEBUG) logger.addHandler(file_handler) logger.addHandler(console_handler) logger.addHandler(syslog_server_handler) def my_fun(n): for i in range(0, n): logging.debug(i) if i == 50: logging.warning("The value of i is 50.") try: i / (50 - i)