當前位置: 首頁>>代碼示例>>Python>>正文


Python handlers.NTEventLogHandler類代碼示例

本文整理匯總了Python中logging.handlers.NTEventLogHandler的典型用法代碼示例。如果您正苦於以下問題:Python NTEventLogHandler類的具體用法?Python NTEventLogHandler怎麽用?Python NTEventLogHandler使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了NTEventLogHandler類的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: initialize_logging

def initialize_logging(logger_name):
    try:
        logging_config = get_logging_config()

        logging.basicConfig(
            format=get_log_format(logger_name),
            level=logging_config['log_level'] or logging.INFO,
        )

        log_file = logging_config.get('%s_log_file' % logger_name)
        if log_file is not None and not logging_config['disable_file_logging']:
            # make sure the log directory is writeable
            # NOTE: the entire directory needs to be writable so that rotation works
            if os.access(os.path.dirname(log_file), os.R_OK | os.W_OK):
                file_handler = logging.handlers.RotatingFileHandler(log_file, maxBytes=LOGGING_MAX_BYTES, backupCount=1)
                formatter = logging.Formatter(get_log_format(logger_name), get_log_date_format())
                file_handler.setFormatter(formatter)

                root_log = logging.getLogger()
                root_log.addHandler(file_handler)
            else:
                sys.stderr.write("Log file is unwritable: '%s'\n" % log_file)

        # set up syslog
        if logging_config['log_to_syslog']:
            try:
                from logging.handlers import SysLogHandler

                if logging_config['syslog_host'] is not None and logging_config['syslog_port'] is not None:
                    sys_log_addr = (logging_config['syslog_host'], logging_config['syslog_port'])
                else:
                    sys_log_addr = "/dev/log"
                    # Special-case BSDs
                    if Platform.is_darwin():
                        sys_log_addr = "/var/run/syslog"
                    elif Platform.is_freebsd():
                        sys_log_addr = "/var/run/log"

                handler = SysLogHandler(address=sys_log_addr, facility=SysLogHandler.LOG_DAEMON)
                handler.setFormatter(logging.Formatter(get_syslog_format(logger_name), get_log_date_format()))
                root_log = logging.getLogger()
                root_log.addHandler(handler)
            except Exception, e:
                sys.stderr.write("Error setting up syslog: '%s'\n" % str(e))
                traceback.print_exc()

        # Setting up logging in the event viewer for windows
        if get_os() == 'windows' and logging_config['log_to_event_viewer']:
            try:
                from logging.handlers import NTEventLogHandler
                nt_event_handler = NTEventLogHandler(logger_name, get_win32service_file('windows', 'win32service.pyd'), 'Application')
                nt_event_handler.setFormatter(logging.Formatter(get_syslog_format(logger_name), get_log_date_format()))
                nt_event_handler.setLevel(logging.ERROR)
                app_log = logging.getLogger(logger_name)
                app_log.addHandler(nt_event_handler)
            except Exception, e:
                sys.stderr.write("Error setting up Event viewer logging: '%s'\n" % str(e))
                traceback.print_exc()
開發者ID:awasilyev,項目名稱:dd-agent,代碼行數:58,代碼來源:config.py

示例2: __init__

 def __init__(self, args=None):
     win32serviceutil.ServiceFramework.__init__(self, args)
     self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
     sys.stdout = sys.stderr = open('nul', 'w')
     self._logger = logging.getLogger('MaxUpdater')
     if servicemanager.RunningAsService():
         ev_net = NTEventLogHandler(self._svc_name_, None)
         ev_net.setLevel(logging.ERROR)
         ev_net.setFormatter(logging.Formatter('%(levelname)-8s %(message)s'))
         self._logger.addHandler(ev_net)
     else:
         logging.basicConfig()
     rl = RotatingFileHandler(os.path.join(self._service_path, 'debug.log'), delay=True, maxBytes=150000,
                              backupCount=5)
     rl.setFormatter(logging.Formatter('%(asctime)s %(name)-12s %(levelname)-8s %(message)s'))
     self._logger.addHandler(rl)
     self._cmd_thread = ExecThread()
     self._update_thread = UpdateThread()
開發者ID:dgsantana,項目名稱:maxupdater,代碼行數:18,代碼來源:updater_service.py

示例3: open_ntlogger

    def open_ntlogger(self):
        from logging.handlers import NTEventLogHandler
        try:
            # setup logger level
            self.logger.setLevel (logging.DEBUG)

            # setup formatter
            fmt = logging.Formatter ("%(asctime)s: %(name)s: %(levelname)s: %(message)s")
            # Create NT Event log handler and set level to INFO
            nh = NTEventLogHandler (self.name)
            nh.setLevel (self.level)
            #add formatter to nh
            nh.setFormatter (fmt)
            #add nh to logger
            self.logger.addHandler (nh)
        except:
            print "Unable to Open NT Eventlog!"
            raise
開發者ID:spdfire,項目名稱:teddix,代碼行數:18,代碼來源:TeddixLogger.py

示例4: add_win_event_log_handler

def add_win_event_log_handler(logger):
    nh = NTEventLogHandler('GISMOH')
    nh.setLevel(DEBUG)
    nh.setFormatter(get_formatter())

    logger.addHandler(nh)
開發者ID:ImperialCollegeLondon,項目名稱:GISMOH_BE,代碼行數:6,代碼來源:Logging.py

示例5: initialize_logging

def initialize_logging(logger_name):
    global windows_file_handler_added
    try:
        logging_config = get_logging_config()

        logging.basicConfig(format=get_log_format(logger_name), level=logging_config["log_level"] or logging.INFO)

        # set up file loggers
        if get_os() == "windows" and not windows_file_handler_added:
            logger_name = "ddagent"
            windows_file_handler_added = True

        log_file = logging_config.get("%s_log_file" % logger_name)
        if log_file is not None and not logging_config["disable_file_logging"]:
            # make sure the log directory is writeable
            # NOTE: the entire directory needs to be writable so that rotation works
            if os.access(os.path.dirname(log_file), os.R_OK | os.W_OK):
                file_handler = logging.handlers.RotatingFileHandler(log_file, maxBytes=LOGGING_MAX_BYTES, backupCount=1)
                formatter = logging.Formatter(get_log_format(logger_name), get_log_date_format())
                file_handler.setFormatter(formatter)

                root_log = logging.getLogger()
                root_log.addHandler(file_handler)
            else:
                sys.stderr.write("Log file is unwritable: '%s'\n" % log_file)

        # set up syslog
        if logging_config["log_to_syslog"]:
            try:
                from logging.handlers import SysLogHandler

                if logging_config["syslog_host"] is not None and logging_config["syslog_port"] is not None:
                    sys_log_addr = (logging_config["syslog_host"], logging_config["syslog_port"])
                else:
                    sys_log_addr = "/dev/log"
                    # Special-case macs
                    if sys.platform == "darwin":
                        sys_log_addr = "/var/run/syslog"

                handler = SysLogHandler(address=sys_log_addr, facility=SysLogHandler.LOG_DAEMON)
                handler.setFormatter(logging.Formatter(get_syslog_format(logger_name), get_log_date_format()))
                root_log = logging.getLogger()
                root_log.addHandler(handler)
            except Exception, e:
                sys.stderr.write("Error setting up syslog: '%s'\n" % str(e))
                traceback.print_exc()

        # Setting up logging in the event viewer for windows
        if get_os() == "windows" and logging_config["log_to_event_viewer"]:
            try:
                from logging.handlers import NTEventLogHandler

                nt_event_handler = NTEventLogHandler(
                    logger_name, get_win32service_file("windows", "win32service.pyd"), "Application"
                )
                nt_event_handler.setFormatter(logging.Formatter(get_syslog_format(logger_name)))
                nt_event_handler.setLevel(logging.ERROR)
                app_log = logging.getLogger(logger_name)
                app_log.addHandler(nt_event_handler)
            except Exception, e:
                sys.stderr.write("Error setting up Event viewer logging: '%s'\n" % str(e))
                traceback.print_exc()
開發者ID:kzw,項目名稱:dd-agent,代碼行數:62,代碼來源:config.py


注:本文中的logging.handlers.NTEventLogHandler類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。