zoukankan      html  css  js  c++  java
  • 说一下syslog日志吧~~~

    # -*- coding:utf-8 -*-
    from logging.handlers import *
    import logging
    import logging.handlers

    class MySocketHandler(SocketHandler):
      def makePickle(self, record):
        return self.format(record) + ' '


    class MyDatagramHandler(DatagramHandler):
      def makePickle(self, record):
        return self.format(record) + ' '


    class MyLog:

    #key为日志里的关键字

      def __init__(self,key):
        LOG_FILE = key +'.log'
        # handler1 = logging.handlers.RotatingFileHandler(LOG_FILE, maxBytes=1024 * 1024, backupCount=5)
        # handler = MyDatagramHandler('192.168.x.xxx', 5000) # 实例化handler
         handler3 = logging.handlers.SysLogHandler(('127.0.0.1', 514), logging.handlers.SysLogHandler.LOG_SYSLOG)#logging 发送到 syslog
        fmt = '%(asctime)s %(levelname)s %(message)s' # 创建输出格式
        formatter = logging.Formatter(fmt) # 实例化formatter
        # handler1.setFormatter(formatter) # 为handler添加formatter
        # handler.setFormatter(formatter)
        handler3.setFormatter(formatter)
        self.logger = logging.getLogger(key) # 获取名为tst的logger,会返回同一个logger对象
        # self.logger.addHandler(handler1)
        # self.logger.addHandler(handler) # 为logger添加handler
        self.logger.addHandler(handler3) #为logger添加fh
        self.logger.setLevel(logging.DEBUG)
      def info(self,msg):
        self.logger.info(msg)
      def error(self,msg):
        self.logger.error(msg)
      def exception(self,msg):
        self.logger.exception(msg)
      def warning(self,msg):
        self.logger.warning(msg)
      def critical(self,msg):
        self.logger.critical(msg)
      def debug(self,msg):
        self.logger.debug(msg)
      def log(self,msg):
        self.logger.log(msg)

  • 相关阅读:
    009---绑定方法与非绑定方法
    008---封装
    007---多态与多态性
    17.出现fatal signal(SIGSEGV),code 1,fault addr 0x0 in tid 29931的问题
    18.让sublime text3支持Vue语法高亮显示
    PNPoly算法代码例子,判断一个点是否在多边形里面
    5.如何调节ubuntu的分辨率
    4.ubuntu实现linux与windows的互相复制与粘贴
    16.遇到就jar mismatch! Fix your dependencies的问题
    15.遇到window leaked的解决方法
  • 原文地址:https://www.cnblogs.com/Adalia-Ting/p/9100846.html
Copyright © 2011-2022 走看看