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()
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()
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
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
def add_win_event_log_handler(logger): nh = NTEventLogHandler('GISMOH') nh.setLevel(DEBUG) nh.setFormatter(get_formatter()) logger.addHandler(nh)
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()
IMPORT_FILE_TYPES = [".xls", ".xlsx"] #Keep the logger and the configuration as global variables logger = None opts = types.SimpleNamespace() # Set up the logger. We won't know what file to log to yet, so we will start by logging # only warnings and above to the Windows event log logger = logging.getLogger() logger.setLevel(logging.DEBUG) formatter = logging.Formatter( '%(asctime)s - %(name)s - %(levelname)s - %(message)s') # Log warnings and above to the windows event log eh = NTEventLogHandler('container_tracking_importer') eh.setLevel(logging.WARNING) eh.setFormatter(formatter) logger.addHandler(eh) class Watcher(win32serviceutil.ServiceFramework): """The Windows service responsible for monitoring the import directory for Excel files. The watcher will use a combination of a polling observer and timed manual wakes to search for new Excel files. Any files found will be passed off to the Importer for processing. """ _svc_name_ = 'container_tracking_importer' _svc_display_name_ = 'Container Tracking Importer' _svc_description_ = 'Import container counts from Excel spreadsheets to an Access database. (v{})'.format( VERSION)