本文整理汇总了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()
示例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()
示例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
示例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)
示例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()