Exemplo n.º 1
0
    def __init__(self, app, action, pid=None, keep_fds=None):
        self.app = app
        self.action = action
        if pid:
            self.pid = pid
        else:
            self.pid = "/var/lock/%s/pid_file.pid" % self.app
        self.pid_dir = os.path.dirname(self.pid)

        if keep_fds:
            self.keep_fds = keep_fds
        else:
            self.keep_fds = []
        # Initialize logging.
        self.logger = logging.getLogger(self.app)
        self.logger.setLevel(logging.DEBUG)
        # Display log messages only on defined handlers.
        self.logger.propagate = False

        # It will work on OS X and Linux. No FreeBSD support, guys, I don't want
        # to import re here to parse your peculiar platform string.
        if sys.platform == "darwin":
            syslog_address = "/var/run/syslog"
        else:
            syslog_address = "/dev/log"

        syslog = handlers.SysLogHandler(syslog_address)
        syslog.setLevel(logging.INFO)

        # Try to mimic to normal syslog messages.
        formatter = logging.Formatter("%(asctime)s %(name)s: %(message)s",
                                      "%b %e %H:%M:%S")
        syslog.setFormatter(formatter)
        self.logger.addHandler(syslog)
Exemplo n.º 2
0
 def setLogger(self):
   # Log entry format
   logFormatter = logging.Formatter("%(asctime)s [%(threadName)-12.12s] [%(levelname)-5.5s]  %(message)s")
   syslogFormatter = logging.Formatter('{{"logger":"optima-logging", "text":"%(message)s", "workflow_id":"{}", "element_id":"{}", "element_type":"{}"}}'.format(
                                       
                                       ))
   # Logger object
   rootLogger = logging.getLogger('job_manager')
   # Logging level at logger level. Set to debug to avoid filtering logs out
   rootLogger.setLevel(logging.DEBUG)
   # Creating handler to write logs on file
   fileHandler = logging.FileHandler(job_manager_LOG_FILE)
   # Adding handler to logger
   fileHandler.setFormatter(logFormatter)
   # Setting file minimal log level for handler
   fileHandler.setLevel(FILE_LOG_LEVEL)
   # STDERR log handler creation, level configuration...
   rootLogger.handlers = []
   # Adding file handler to logger
   rootLogger.addHandler(fileHandler)
   # Appending STDERR log handler to logger
   # rootLogger.addHandler(streamHandler)
   syslogHandler = handlers.SysLogHandler('/dev/log')
   syslogHandler.setFormatter(syslogFormatter)
   fileHandler.setLevel(FILE_LOG_LEVEL)
   rootLogger.addHandler(syslogHandler)
   return rootLogger 
 def setLogger(self):
     agent_name = CONFIGURATION_SENDER_NAME.lower()
     # Log entry format
     logFormatter = logging.Formatter(
         "%(asctime)s [%(threadName)-12.12s] [%(levelname)-5.5s]  %(message)s"
     )
     syslogFormatter = logging.Formatter(
         '{{"logger":"optima-logging", "text":"%(message)s", "agent_type":"{}","device_name":"{}","task_id":"{}","job_id":"{}",       "job_name":"{}"}}'
         ''.format(agent_name, self.device.name, self.task.id, self.job.id,
                   self.job.name))
     # Logger object
     rootLogger = logging.getLogger(CONFIGURATION_SENDER_NAME)
     # Logging level at logger level. Set to debug to avoid filtering logs out
     rootLogger.setLevel(logging.DEBUG)
     # Creating handler to write logs on file
     fileHandler = logging.FileHandler(CONFIGURATION_SENDER_NAME)
     # Adding handler to logger
     fileHandler.setFormatter(logFormatter)
     # Setting file minimal log level for handler
     fileHandler.setLevel(FILE_LOG_LEVEL)
     # STDERR log handler creation, level configuration...
     rootLogger.handlers = []
     # Adding file handler to logger
     rootLogger.addHandler(fileHandler)
     # Appending STDERR log handler to logger
     # rootLogger.addHandler(streamHandler)
     syslogHandler = handlers.SysLogHandler('/dev/log')
     syslogHandler.setFormatter(syslogFormatter)
     fileHandler.setLevel(FILE_LOG_LEVEL)
     rootLogger.addHandler(syslogHandler)
     return rootLogger
Exemplo n.º 4
0
    def init_vars(self):
        self.project_table = {'': ''}  # Empty table
        self.project_titles = sorted(
            self.project_table.keys())  # Get a sorted list of the keys

        self.branches = ['master', 'testing']
        # self.generators = ['Unity', 'C++', 'LaTeX', 'R']
        self.generators = generators.Generators

        if sys.platform == 'darwin':
            self.project_root = '/home/'
        else:
            self.project_root = 'C:\\'

        # Set the logger
        if sys.platform == 'darwin':
            self.logger = logging.getLogger()
            syslogH = handlers.SysLogHandler(address='/var/run/syslog',
                                             facility='local1')
            syslogH.ident = 'updater_application:'
            self.logger.addHandler(syslogH)
        else:  # Handle the Windows case
            self.logger = logging.getLogger('updater_application')

        self.logger.setLevel(logging.INFO)
Exemplo n.º 5
0
def syslog_hdlr(host='localhost', port=None, tcp=False, **kwargs):
    """A syslog log handler

    Args:
        host (string): The host name (default: localhost).

        port (int): The port (default: `logging.handlers` default).

        tcp (bool): Create a TCP connection instead of UDP (default: False).

    Returns:
        New instance of :class:`logging.handlers.SysLogHandler`

    Examples:
        >>> syslog_hdlr()  # doctest: +ELLIPSIS
        <logging.handlers.SysLogHandler object at 0x...>
    """
    if tcp:
        def_port = logging.handlers.SYSLOG_TCP_PORT
        socktype = socket.SOCK_STREAM
    else:
        def_port = logging.handlers.SYSLOG_UDP_PORT
        socktype = socket.SOCK_DGRAM

    address = (host, port or def_port)
    return hdlrs.SysLogHandler(address, socktype=socktype)
Exemplo n.º 6
0
def get_handler(debug=False,
                syslog=False,
                logfile=None,
                rotation_parameters=None):
    """get an apropriate handler according to given parameters"""
    if os.environ.get('APYCOT_ROOT'):
        handler = logging.StreamHandler(sys.stdout)
    if debug:
        handler = logging.StreamHandler()
    elif logfile is None:
        if syslog:
            from logging import handlers
            handler = handlers.SysLogHandler()
        else:
            handler = logging.StreamHandler()
    else:
        try:
            if rotation_parameters is None:
                if os.name == 'posix' and sys.version_info >= (2, 6):
                    from logging.handlers import WatchedFileHandler
                    handler = WatchedFileHandler(logfile)
                else:
                    handler = logging.FileHandler(logfile)
            else:
                from logging.handlers import TimedRotatingFileHandler
                handler = TimedRotatingFileHandler(logfile,
                                                   **rotation_parameters)
        except IOError:
            handler = logging.StreamHandler()
    return handler
Exemplo n.º 7
0
    def __init__(self,
                 app,
                 pid,
                 action,
                 keep_fds=None,
                 auto_close_fds=True,
                 privileged_action=None,
                 user=None,
                 group=None,
                 verbose=False,
                 logger=None,
                 foreground=False,
                 chdir="/"):
        self.app = app

        # Allow no pid file.
        if pid is not None:
            self.pid = os.path.abspath(pid)
        else:
            self.pid = None

        self.action = action
        self.keep_fds = keep_fds or []
        self.privileged_action = privileged_action or (lambda: ())
        self.user = user
        self.group = group
        self.logger = logger
        self.verbose = verbose
        self.auto_close_fds = auto_close_fds
        self.foreground = foreground
        self.chdir = chdir

        if self.logger is None:
            # Initialize logging.
            self.logger = logging.getLogger(self.app)
            self.logger.setLevel(logging.DEBUG)
            # Display log messages only on defined handlers.
            self.logger.propagate = False

            # Initialize syslog.
            # It will correctly work on OS X, Linux and FreeBSD.
            if sys.platform == "darwin":
                syslog_address = "/var/run/syslog"
            else:
                syslog_address = "/dev/log"

            # We will continue with syslog initialization only if actually have
            # such capabilities on the machine we are running this.
            if os.path.exists(syslog_address):
                syslog = handlers.SysLogHandler(syslog_address)
                if self.verbose:
                    syslog.setLevel(logging.DEBUG)
                else:
                    syslog.setLevel(logging.INFO)
                # Try to mimic to normal syslog messages.
                formatter = logging.Formatter(
                    "%(asctime)s %(name)s: %(message)s", "%b %e %H:%M:%S")
                syslog.setFormatter(formatter)

                self.logger.addHandler(syslog)
Exemplo n.º 8
0
    def init(self, *priv):
        if len(priv) < 1:
            raise error.SnmpsimError(
                'Bad syslog params, need at least facility, also accept priority, host, port, socktype (tcp|udp)'
            )
        if len(priv) < 2:
            priv = [priv[0], 'debug']
        if len(priv) < 3:
            priv = [priv[0], priv[1], 'localhost', 514, 'udp']
        if not priv[2].startswith('/'):
            if len(priv) < 4:
                priv = [priv[0], priv[1], priv[2], 514, 'udp']
            if len(priv) < 5:
                priv = [priv[0], priv[1], priv[2], 514, 'udp']
            priv = [priv[0], priv[1], priv[2], int(priv[3]), priv[4]]

        try:
            handler = handlers.SysLogHandler(
                priv[2].startswith('/') and priv[2] or (priv[2], int(priv[3])),
                priv[0].lower(),
                len(priv) > 4 and priv[4] == 'tcp' and socket.SOCK_STREAM
                or socket.SOCK_DGRAM)

        except:
            raise error.SnmpsimError('Bad syslog option(s): %s' %
                                     sys.exc_info()[1])
        handler.setFormatter(
            logging.Formatter('%(asctime)s %(name)s: %(message)s'))
        self._logger.addHandler(handler)
Exemplo n.º 9
0
 def __init__(self, *a, **k):
     Configuration.__init__(self, *a, **k)
     self.host, self.port, self.facility, self.proto = None, None, None, None
     for default in filter(lambda x: x.startswith('DEFAULT_'),
                           vars(SyslogConfiguration).keys()):
         # transform name to corresponding property in config file
         prop = default.replace('DEFAULT_', 'syslog_').lower()
         name = prop[7:]  # strip 'syslog_' prefix
         # set attr to value in config, or default
         value = self._obj.get(prop, getattr(self, default))
         setattr(self, name, value)
     self.port = int(self.port)
     self.proto = self.proto.lower()
     # validation
     h = handlers.SysLogHandler()
     try:
         h.encodePriority(self.facility, 'info')
     except KeyError:
         raise ValueError(
             "Invalid syslog facility '{}', must be one of: {}".format(
                 self.facility, ', '.join(h.facility_names)))
     if not self.proto in ('tcp', 'udp'):
         raise ValueError("Invalid syslog protocol '{}', must be either "
                          "'udp' or 'tcp'")
     # keyword args for logging.SysLogHandler constructor
     self.handler_args = {
         'address': (self.host, self.port),
         'facility': self.facility,
         'socktype': {
             'tcp': socket.SOCK_STREAM,
             'udp': socket.SOCK_DGRAM
         }[self.proto]
     }
Exemplo n.º 10
0
def _syslog(**kwargs):
    formating = kwargs.get('format', SHORT)
    handler = handlers.SysLogHandler(
        address=kwargs.get('address', '/dev/log'),
        facility=kwargs.get('facility', 'syslog'),
    )
    handler.setFormatter(logging.Formatter(formating))
    return handler
Exemplo n.º 11
0
def configure_logging():
    logger = logging.getLogger()
    logger.setLevel(logging.INFO)
    formatter = logging.Formatter(
        "face-id-network-connectivity:%(asctime)s:%(levelname)s:%(filename)s:%(funcName)s:%(lineno)d:%(message)s",
        datefmt='%Y-%m-%d %H:%M:%S')
    syslog_handler = handlers.SysLogHandler(address='/dev/log')
    syslog_handler.setFormatter(formatter)
    logger.addHandler(syslog_handler)
Exemplo n.º 12
0
def configure_default_logging(**kwargs):
    if not kwargs:
        logger = logging.getLogger()
        formatter = LunrFormatter(LOG_FORMAT)
        handler = handlers.SysLogHandler('/dev/log',
                                         handlers.SysLogHandler.LOG_USER)
        handler.setFormatter(formatter)
        logger.addHandler(handler)
    else:
        logging.basicConfig(**kwargs)
Exemplo n.º 13
0
    def send_syslog(self, host, port, msg):
        logger = logging.getLogger()
        logger.setLevel(logging.DEBUG)
        # use TCP; requires Python 3.2+
        handler = handlers.SysLogHandler(address=(host, port), socktype=socket.SOCK_STREAM)
        logger.addHandler(handler)

        # include the current time so that it doesn't get mistaken for previous test runs
        msg = "test message from Python unittest, at {0}".format(int(time.time()))
        logger.info(msg)
Exemplo n.º 14
0
def get_pbench_logger(caller, config):
    """Add a specific handler for the caller using the configured LOGSDIR.

    We also return a logger that supports "brace" style message formatting,
    e.g. logger.warning("that = {}", that)
    """

    pbench_logger = logging.getLogger(caller)
    if caller not in _handlers:
        try:
            logging_level = config.get(caller, "logging_level")
        except (NoSectionError, NoOptionError):
            logging_level = config.default_logging_level
        pbench_logger.setLevel(logging_level)

        if config.logger_type == "file":
            logdir = config.LOGSDIR / caller
            try:
                logdir.mkdir()
            except FileExistsError:
                # directory already exists, ignore
                pass
            handler = logging.FileHandler(logdir / f"{caller}.log")
        elif config.logger_type == "devlog":
            handler = handlers.SysLogHandler(address="/dev/log")
        elif (config.logger_type == "hostport"
              ):  # hostport logger type uses UDP-based logging
            handler = handlers.SysLogHandler(address=(config.logger_host,
                                                      int(config.logger_port)))
        else:
            raise Exception("Unsupported logger type")

        handler.setLevel(logging.DEBUG)
        if not config._unittests:
            logfmt = "{asctime} {levelname} {process} {thread} {name}.{module} {funcName} {lineno} -- {message}"
        else:
            logfmt = "1970-01-01T00:00:42.000000 {levelname} {name}.{module} {funcName} -- {message}"
        formatter = _PbenchLogFormatter(fmt=logfmt)
        handler.setFormatter(formatter)
        _handlers[caller] = handler
        pbench_logger.addHandler(handler)
    return _StyleAdapter(pbench_logger)
def main():
    logger = logging.getLogger('docker-volume-discover')
    logger.setLevel(os.environ.get('HIVE_LOG_LEVEL', logging.INFO))
    syslog_handler = handlers.SysLogHandler(
        address="/dev/log", facility=handlers.SysLogHandler.LOG_LOCAL1)
    logger.addHandler(syslog_handler)
    try:
        client = docker.from_env()
        print(json.dumps(dict(data=[v for v in volumes(client, logger)])))
    except docker.errors.APIError as e:
        logger.exception(f'fail to initialize docker client: {e}')
Exemplo n.º 16
0
 def create_syslog_handler() -> logging.Handler:
     """
     Create a SyslogHandler
     :return: SyslogHandler
     """
     try:
         return handlers.SysLogHandler(address='/dev/log')
     except Exception as ex:
         logging.warning("Logger can not log on {0}: {1}".format(
             '/dev/log', ex))
         return None
Exemplo n.º 17
0
def main():
  parser = argparse.ArgumentParser()
  parser.add_argument('servers', metavar='Server', nargs='+',
                      help='list of docker swarm node server')
  group = parser.add_mutually_exclusive_group()
  group.add_argument("--discover", help="discover services and print zabbix discover data format json.", action="store_true")
  group.add_argument("--discover-standalone", help="discover standalone type services and print zabbix discover data format json.", action="store_true")
  group.add_argument("--discover-innerservice",
                     help="discover inner-services in a standalone type container and print zabbix discover data format json.", action="store_true")
  group.add_argument("--uptime", help="print least uptime for the service.", metavar='service')
  group.add_argument("--replicas", help="print available running task persentage for the service.", metavar='service')
  parser.add_argument("--inner", help="name of inner service for standalone container.", metavar='inner')
  parser.add_argument("--dispose", help="number of cache entry refreleshed.", metavar='nDispose', type=int, default=0)
  group.add_argument("--failed-innerservice-count",
                     help="count number of failed serivces in standalone type container and print", metavar='service')
  args = parser.parse_args()
  logger = logging.getLogger('docker-service')
  logger.setLevel(os.environ.get('HIVE_LOG_LEVEL', logging.INFO))
  syslog_handler = handlers.SysLogHandler(address="/dev/log", facility=handlers.SysLogHandler.LOG_LOCAL2)
  logger.addHandler(syslog_handler)
  clients = {}
  dot_docker = os.environ['HOME'] + '/.docker'
  tls_config = docker.tls.TLSConfig(ca_cert=dot_docker + '/ca.pem', verify=dot_docker + '/ca.pem',
                                    client_cert=(dot_docker + '/cert.pem', dot_docker + '/key.pem'))
  for server in args.servers:
    try:
      client = docker.DockerClient(base_url=f'tcp://{server}:2376', tls=tls_config)
      info = client.info()
    except (docker.errors.APIError, docker.errors.DockerException) as e:
      logger.error(f'fail to initialize docker client for server {server}: {e}')
      continue
    node_id = info.get('Swarm', {}).get('NodeID', '')
    if len(node_id) > 0:
      clients[node_id] = client
    else:
      logger.error(f'fail to get node id for server {server}')

  if args.discover:
    print(json.dumps(dict(data=[v for v in discover(next(iter(clients.values())), logger)])))
  elif args.discover_standalone:
    print(json.dumps(dict(data=[v for v in discover(next(iter(clients.values())), logger, standalone=True)])))
  elif args.discover_innerservice:
    print(json.dumps(dict(data=[v for v in discover_innerservice(clients, logger, args.dispose)])))
  elif args.uptime:
    if args.inner:
      print(json.dumps(service_uptime_innerservice(clients, logger, args.uptime, args.inner.replace('%', '@'))))
    else:
      print(json.dumps(service_uptime(next(iter(clients.values())), logger, args.uptime)))
  elif args.replicas:
    print(json.dumps(replicas(next(iter(clients.values())), logger, args.replicas)))
  elif args.failed_innerservice_count:
    print(json.dumps(failed_innerservice_count(clients, logger, args.failed_innerservice_count)))
  else:
    logger.error('command option is required')
Exemplo n.º 18
0
def setup(progname,
          logfile,
          syslog_verbosity=0,
          terminal_verbosity=0,
          logfile_verbosity=0):
    """define what type of messages will be logged and how """
    ### get root logger and set log system
    #root_logger =logger.getChild(progname)
    #root_logger=logging.getLogger(__name__)
    #root_logger = logging.getLogger(progname)
    #root_logger=logging.getLogger()
    #logging.basicConfig(filename='example.log',level=logging.DEBUG)
    root_logger.setLevel(
        logging.NOTSET
    )  # default is logging.WARNING so need to change it for getting all logs
    if syslog_verbosity == 0 and terminal_verbosity == 0 and logfile_verbosity == 0:
        ### if all verbosity are at zero, all logs are ignored
        nh = logging.NullHandler()
        nh.setLevel(logging.NOTSET)
        root_logger.addHandler(nh)
    else:
        logging_levels = (None, logging.CRITICAL, logging.ERROR,
                          logging.WARNING, logging.INFO, logging.DEBUG,
                          logging.NOTSET)
        if syslog_verbosity > 0:
            ### logs go on syslog
            sh = handlers.SysLogHandler(
                address='/dev/log'
            )  #If not specified localhost:514 is used. Better than system dependent /dev/log on Linux /var/run/syslog on OS/X
            sh.setFormatter(
                logging.Formatter("{} %(message)s".format(progname)))
            sh.setLevel(logging_levels[syslog_verbosity])
            root_logger.addHandler(sh)
        if terminal_verbosity > 0:
            ### logs go on stderr terminal
            th = logging.StreamHandler(sys.stderr)
            th.setFormatter(logging.Formatter("【%(levelname)s】:%(message)s"))
            th.setLevel(logging_levels[terminal_verbosity])
            root_logger.addHandler(th)
        if logfile_verbosity > 0:
            ### logs go on logfile
            ### the logfile is deleted at each start, good for limit the file size,and reducing time for auditing the logs.
            ### (still possible to do backup but Syslog is better for keeping logs a longtime)
            fh = logging.FileHandler(logfile,
                                     mode='w',
                                     encoding='utf-8',
                                     delay=False)
            fh.setFormatter(
                logging.Formatter(
                    '%(created)0.3f\t%(levelname)s\t%(message)s'))
            fh.setLevel(logging_levels[logfile_verbosity])
            root_logger.addHandler(fh)
Exemplo n.º 19
0
def setup_logging():
    global logger

    h = sh.SysLogHandler(address=('localhost', 514),
                         facility=sh.SysLogHandler.LOG_LOCAL5)
    formatter = logging.Formatter(
        '%(name)s[%(process)s]: [%(filename)s:%(lineno)s - %(funcName)s()]: %(message)s'
    )
    h.setFormatter(formatter)
    logger.setLevel(logging.INFO)
    logger.addHandler(h)

    return logger
Exemplo n.º 20
0
def setup_logger(project, cfg):

    if _loggers.get(project):
        return

    logger = getLogger(None)

    # Set logging level.
    level = LOG_LEVELS.get(cfg.dfa_log.log_level.lower(), logging.WARNING)
    logger.setLevel(level)

    # Set default log levels for specified modules.
    def_log_levels = cfg.dfa_log.default_log_levels
    for dll in def_log_levels.split(','):
        mod, sep, lvl = dll.partition('=')
        level = LOG_LEVELS.get(lvl.lower(), logging.WARNING)
        logging.getLogger(mod).setLevel(level)

    # Set log file path name.
    log_dir = cfg.dfa_log.log_dir
    if cfg.dfa_log.log_file_prefix:
        log_file = "%s_%s.log" % (cfg.dfa_log.log_file_prefix, project)
    else:
        log_file = cfg.dfa_log.log_file

    if log_dir and log_file:
        try:
            if not os.path.exists(log_dir):
                os.makedirs(log_dir)
        except OSError:
            pass
        else:
            log_file_path = os.path.join(log_dir, log_file)
            handler = log_hdlr.RotatingFileHandler(log_file_path,
                                                   maxBytes=MAX_BYTES,
                                                   backupCount=BACKUP_COUNT)
    else:
        handler = logging.StreamHandler(sys.stdout)

    if cfg.dfa_log.use_syslog:
        log_fac = cfg.dfa_log.syslog_log_facility
        facility = getattr(log_hdlr.SysLogHandler, log_fac, None)
        handler = log_hdlr.SysLogHandler(address='/dev/log', facility=facility)

    # Setting log format.
    log_format = cfg.dfa_log.log_format
    date_fowrmat = cfg.dfa_log.log_date_format
    formatter = logging.Formatter(fmt=log_format, datefmt=date_fowrmat)
    handler.setFormatter(formatter)
    logger.addHandler(handler)
    _loggers[project] = logger
Exemplo n.º 21
0
    def setup(opts=None, phase=None):
        #TODO: remove ClientOptionParser, pass in opts always
        opts = opts if opts else ClientOptionParser(phase=phase).parse_args()[0]

        # if a logging config file exists, we will use that...
        logging_config_file = opts.get(ClientOption.LOGGING_CONFIG_FILE)
        if os.path.exists(logging_config_file):
            config.fileConfig(logging_config_file)
            return logging.getLogger()

        # otherwise add default handlers etc...
        logger = logging.getLogger()
        logger.filters = []
        logger.handlers = []

        log_level = opts.get(ClientOption.LOG_LEVEL)
        if phase == 'install' or opts.get(ClientOption.DEBUG):
            log_level = logging.DEBUG
        logger.setLevel(log_level)

        context_info = ' %(app_id)s (%(version)s) %(device_id)s %(device_name)s %(user_id)s %(user_name)s %(badge_code)s'

        # dev/log
        sys_log_file = '/var/log/syslog'
        if os.path.exists(sys_log_file):
            sys_log_handler = handlers.SysLogHandler('/dev/log')
            formatter = logging.Formatter('%(message)s' + context_info)
            sys_log_handler.setFormatter(formatter)
            sys_log_handler.addFilter(ContextFilter(opts))
            logger.addHandler(sys_log_handler)

        # console
        logger.addHandler(logging.StreamHandler())

        # fileHandler
        try:
            log_file = opts.get(ClientOption.LOG_FILE)
            if log_file:
                if not os.path.exists(os.path.dirname(log_file)):
                    os.makedirs(os.path.dirname(log_file))
                file_handler = handlers.TimedRotatingFileHandler(log_file, when='D', interval=1, backupCount=7)
                formatter = logging.Formatter('%(asctime)s %(message)s' + context_info, datefmt='%FT%T')
                file_handler.setFormatter(formatter)
                file_handler.addFilter(ContextFilter(opts))
                logger.addHandler(file_handler)
        except Exception as e:
            logger.exception(e)

        return logger
Exemplo n.º 22
0
    def setup_logger(log_level=logging.INFO, enable_update_logging=False):
        """
        Setup the OpenMotics logger.
        :param log_level: Sets the main log level for OpenMotics logging to the default StreamHandler/SysLogHandler
        :param enable_update_logging: Enables logging to the `update_log` file. This will always log in DEBUG
        """

        import constants
        from platform_utils import System

        # Remove all log handlers (since python2 `defaultConfig` has no `force` flag)
        root_logger = logging.getLogger()
        while root_logger.handlers:
            root_logger.removeHandler(root_logger.handlers[0])

        # Setup basic stream handler
        logging.basicConfig(format=Logs.LOG_FORMAT, level=logging.INFO)
        openmotics_log_level = log_level

        # Alter some system loggers
        requests_logger = logging.getLogger(
            'requests.packages.urllib3.connectionpool')
        requests_logger.setLevel(logging.WARNING)

        update_handler = None
        if enable_update_logging:
            update_handler = handlers.RotatingFileHandler(
                constants.get_update_log_location(),
                maxBytes=3 * 1024**2,
                backupCount=2)
            update_handler.setLevel(logging.DEBUG)
            update_handler.setFormatter(logging.Formatter(Logs.LOG_FORMAT))
            openmotics_log_level = min(log_level, logging.DEBUG)

        syslog_handler = None
        if System.get_operating_system().get('ID') == System.OS.BUILDROOT:
            syslog_handler = handlers.SysLogHandler(address='/dev/log')
            syslog_handler.setLevel(log_level)
            syslog_handler.setFormatter(logging.Formatter(Logs.LOG_FORMAT))

        for logger_namespace in ['openmotics', 'gateway']:
            _logger = logging.getLogger(logger_namespace)
            _logger.setLevel(openmotics_log_level)
            _logger.propagate = True

            for extra_handler in [update_handler, syslog_handler]:
                # Add extra handlers, where available
                if extra_handler is not None:
                    _logger.addHandler(extra_handler)
Exemplo n.º 23
0
 def log_init(self):
     self.logger = logging.getLogger(
         'dbpc module - service:%s - component:%s' %
         (self.service, self.component))
     sh = None
     if self.log_obj is None:
         sh = hd.SysLogHandler(address='/dev/log',
                               facility=hd.SysLogHandler.LOG_LOCAL3)
     else:
         sh = logging.StreamHandler(self.log_obj)
     formatter = logging.Formatter(
         '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
     sh.setFormatter(formatter)
     self.logger.setLevel(logging.INFO)
     self.logger.addHandler(sh)
Exemplo n.º 24
0
def configure_logging(logger, log_to_syslog=False):
    if log_to_syslog:
        syslog_log = handlers.SysLogHandler(address="/dev/log")
        log_format_syslog = logging.Formatter(time.strftime("%b %d %H:%M:%S") + " " +
                                              socket.gethostname() + " " +
                                              str("ResultWriter") + "[" +
                                              str(os.getpid()) + "]: " +
                                              "%(levelname)s %(message)s")
        syslog_log.setFormatter(log_format_syslog)
        logger.addHandler(syslog_log)
    else:
        console_log = logging.StreamHandler()
        log_format_console = logging.Formatter('[%(asctime)s] %(levelname)s %(message)s')
        console_log.setFormatter(log_format_console)
        logger.addHandler(console_log)
Exemplo n.º 25
0
def setup_logger(**params):
    params['level'] = parse_level(params['level'].lower())
    if not 'format' in params:
        params[
            'format'] = "%(asctime)s %(name)-8s %(levelname)-8s | %(message)s"
    if params['type'] == 'file':
        handler = logging.FileHandler(params['filename'])
    elif params['type'] == 'stderr':
        handler = logging.StreamHandler()
    elif params['type'] == 'syslog':
        handler = handlers.SysLogHandler(address='/dev/log')
    logger.setLevel(params['level'])
    handler.setLevel(params['level'])
    handler.setFormatter(logging.Formatter(params['format']))
    logger.addHandler(handler)
Exemplo n.º 26
0
    def make_handler_rsyslog(self, rsyslog_host, rsyslog_port, log_level):
        """
        \brief (Internal usage) Prepare logging via rsyslog
        \param rsyslog_host The hostname of the rsyslog server
        \param rsyslog_port The port of the rsyslog server
        \param log_level Log level
        """
        # Prepare the handler
        shandler = handlers.SysLogHandler(
            (rsyslog_host, rsyslog_port),
            facility=handlers.SysLogHandler.LOG_DAEMON)

        # The log file must remain open while daemonizing
        self.prepare_handler(shandler, log_level)
        return shandler
Exemplo n.º 27
0
def syslog_hdlr(host='localhost', port=None, tcp=False, **kwargs):
    """A syslog log handler

    Args:
        host (string): The host name (default: localhost). Set to None to use
            the platform dependent domain socket.

        port (int): The port (default: `logging.handlers` default).

        tcp (bool): Create a TCP connection instead of UDP (default: False).

    Returns:
        New instance of :class:`logging.handlers.SysLogHandler`

    Examples:
        >>> syslog_hdlr()  # doctest: +ELLIPSIS
        <logging.handlers.SysLogHandler object at 0x...>
    """
    # http://stackoverflow.com/a/13874620/408556
    DEF_SOCKETS = {'linux2': '/dev/log', 'darwin': '/var/run/syslog'}

    if tcp:
        def_port = hdlrs.SYSLOG_TCP_PORT
        socktype = socket.SOCK_STREAM
    else:
        def_port = hdlrs.SYSLOG_UDP_PORT
        socktype = socket.SOCK_DGRAM

    if kwargs.get('address'):
        address = kwargs['address']
    elif host:
        address = (host, port or def_port)
    elif sys.platform in DEF_SOCKETS:
        address = DEF_SOCKETS[sys.platform]
    else:
        msg = 'Domain socket location for {} is not supported.'
        raise ValueError(msg.format(sys.platform))

    if kwargs.get('facility'):
        facility = kwargs['facility']
    elif kwargs.get('local_num') and 8 > kwargs['local_num'] >= 0:
        # http://unix.stackexchange.com/a/146993
        value = 'LOG_LOCAL{}'.format(kwargs['facility'])
        facility = getattr(hdlrs.SysLogHandler, value)
    else:
        facility = hdlrs.SysLogHandler.LOG_USER

    return hdlrs.SysLogHandler(address, facility=facility, socktype=socktype)
Exemplo n.º 28
0
 def setup(self, level='info', format=None, type='stderr', filename=None):
     """ By default, will log on stderr from lvl DEBUG """
     level = getattr(logging, level.upper())
     if format is None:
         format = Logger.DEFAULT_FORMAT
     if type == 'file':
         handler = logging.FileHandler(filename)
     elif type == 'stderr':
         handler = logging.StreamHandler()
     elif type == 'syslog':
         handler = handlers.SysLogHandler(
             address=Logger.DEFAULT_SYSLOG_ADDRESS)
     handler.setLevel(level)
     handler.setFormatter(logging.Formatter(format))
     self.logger.setLevel(level)
     self.logger.addHandler(handler)
     self.logger.propagate = False
Exemplo n.º 29
0
def initLog(level=l.DEBUG):
    global initted
    if not initted:
        initted = True
        root = l.getLogger()
        root.setLevel(level)
        errHandler = h.SysLogHandler("/dev/log")
        errHandler.setLevel(l.ERROR)
        f = l.Formatter(
            "%(name)s [%(module)s/%(funcName)s/%(lineno)d] %(levelname)s: %(message)s"
        )
        errHandler.setFormatter(f)
        root.addHandler(errHandler)
        othHandler = l.StreamHandler()
        othHandler.setFormatter(l.Formatter(l.BASIC_FORMAT))
        othHandler.setLevel(level)
        root.addHandler(othHandler)
Exemplo n.º 30
0
def setup(name):
    logging.setLoggerClass(PyLogrus)

    # Set root log level - higher handlers can set their own filter level
    logging.root.setLevel(logging.DEBUG)
    log = logging.getLogger(name)

    if log.hasHandlers():
        handler = log.handlers[0]
    else:
        # Add our handler
        handler = logging_handlers.SysLogHandler(address='/dev/log')
        handler.setFormatter(
            TextFormatter(fmt='%(levelname)s %(message)s', colorize=False))
        log.addHandler(handler)

    return log, handler