
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
3
yb#                 @   s|   d Z ddlZddlZddlZddlZddlZddlZddlZdadddZ	ddd	Z
d
d ZdddZG dd dejZdS )a  logging facilities

The way to use this is as follows:

* each module declares its own logger, using:

    from .logger import create_logger
    logger = create_logger()

* then each module uses logger.info/warning/debug/etc according to the
  level it believes is appropriate:

    logger.debug('debugging info for developers or power users')
    logger.info('normal, informational output')
    logger.warning('warn about a non-fatal error or sth else')
    logger.error('a fatal error')

  ... and so on. see the `logging documentation
  <https://docs.python.org/3/howto/logging.html#when-to-use-logging>`_
  for more information

* console interaction happens on stderr, that includes interactive
  reporting functions like `help`, `info` and `list`

* ...except ``input()`` is special, because we can't control the
  stream it is using, unfortunately. we assume that it won't clutter
  stdout, because interaction would be broken then anyways

* what is output on INFO level is additionally controlled by commandline
  flags
    NFc             C   s(   dj |||j| }tt}|j| d S )Nz{0}:{1}: {2}: {3})format__name__create_loggerwarning)messagecategoryfilenamelinenofilelinemsglogger r   /usr/lib64/python3.6/logger.py_log_warning/   s    r   BORG_LOGGING_CONFinfoc             C   s  d}|rt jj||}|rydt jj|}t|}tjj| W dQ R X da	tj
t}tj
d}	||	_|jdj| tt_dS  tk
r }
 zt|
}W Y dd}
~
X nX tj
d}tj| }|r| rd}nd}|rt|ntj|}|j| tj
d}	||	_||	_t	r.|jr.|jd j  |jj  |j| |j|j  da	tj
t}|rl|jd	j|| |jd
 tt_|S )a  setup logging module according to the arguments provided

    if conf_fname is given (or the config file name can be determined via
    the env_var, if given): load this logging configuration.

    otherwise, set up a stream handler logger on stderr (by default, if no
    stream is provided).

    if is_serve == True, we configure a special log format as expected by
    the borg client log message interceptor.
    NTZborgz+using logging configuration read from "{0}" z/$LOG %(levelname)s %(name)s Remote: %(message)sz%(message)sr   z*setup_logging for "{0}" failed with "{1}".z,using builtin fallback logging configuration) osenvirongetpathabspathopenloggingconfigZ
fileConfig
configured	getLoggerr   jsondebugr   r   warningsshowwarning	ExceptionstrZStreamHandlerJsonFormatter	FormatterZsetFormatter	formatterZhandlerscloseclearZ
addHandlersetLevelupperr   )streamZ
conf_fnameZenv_varlevelZis_server   Zerr_msgfr   Zborg_loggererrZhandlerZfmtr&   r   r   r   setup_logging8   sN    











r/   c              C   s\   yBt j j} t j| }x&|dks*|jtkr<| j} t j| }qW |jS  tk
rV   tS X dS )zfind the name of the first module calling this module

    if we cannot find it, we return the current module's name
    (__name__) instead.
    N)inspectZcurrentframef_backZ	getmoduler   AttributeError)framemoduler   r   r   find_parent_modulew   s    

r5   c                s   G  fddd  | S )a  lazily create a Logger object with the proper path, which is returned by
    find_parent_module() by default, or is provided via the commandline

    this is really a shortcut for:

        logger = logging.getLogger(__name__)

    we use it to avoid errors and provide a more standard API.

    We must create the logger lazily, because this is usually called from
    module level (and thus executed at import time - BEFORE setup_logging()
    was called). By doing it lazily we can do the setup first, we just have to
    be careful not to call any logger methods before the setup_logging() call.
    If you try, you'll get an exception.
    c                   sn   e Zd ZdddZedd Z fddZdd	 Zd
d Zdd Z	dd Z
dd Zdd Zdd Zdd ZdS )z!create_logger.<locals>.LazyLoggerNc             S   s   |pt  | _d | _d S )N)r5   _LazyLogger__name_LazyLogger__real_logger)selfnamer   r   r   __init__   s    z*create_logger.<locals>.LazyLogger.__init__c             S   sP   | j d krJtstdtj| j| _ | jjdrJ| j jtjkrJ| j j	d | j S )Nz8tried to call a logger before setup_logging() was calledzborg.debug.ZWARNING)
r7   r   r"   r   r   r6   
startswithr,   ZNOTSETr)   )r8   r   r   r   Z__logger   s    
z*create_logger.<locals>.LazyLogger.__loggerc                s    | j d | S )N.)r6   )r8   suffix)
LazyLoggerr   r   getChild   s    z*create_logger.<locals>.LazyLogger.getChildc             _   s   | j j||S )N)_LazyLogger__loggerr)   )r8   argskwr   r   r   r)      s    z*create_logger.<locals>.LazyLogger.setLevelc             _   s,   d|kr|j d|jdi d< | jj||S )Nmsgidextra)pop
setdefaultr@   log)r8   rA   rB   r   r   r   rG      s    z%create_logger.<locals>.LazyLogger.logc             _   s,   d|kr|j d|jdi d< | jj||S )NrC   rD   )rE   rF   r@   	exception)r8   rA   rB   r   r   r   rH      s    z+create_logger.<locals>.LazyLogger.exceptionc             _   s,   d|kr|j d|jdi d< | jj||S )NrC   rD   )rE   rF   r@   r   )r8   rA   rB   r   r   r   r      s    z'create_logger.<locals>.LazyLogger.debugc             _   s,   d|kr|j d|jdi d< | jj||S )NrC   rD   )rE   rF   r@   r   )r8   rA   rB   r   r   r   r      s    z&create_logger.<locals>.LazyLogger.infoc             _   s,   d|kr|j d|jdi d< | jj||S )NrC   rD   )rE   rF   r@   r   )r8   rA   rB   r   r   r   r      s    z)create_logger.<locals>.LazyLogger.warningc             _   s,   d|kr|j d|jdi d< | jj||S )NrC   rD   )rE   rF   r@   error)r8   rA   rB   r   r   r   rI      s    z'create_logger.<locals>.LazyLogger.errorc             _   s,   d|kr|j d|jdi d< | jj||S )NrC   rD   )rE   rF   r@   critical)r8   rA   rB   r   r   r   rJ      s    z*create_logger.<locals>.LazyLogger.critical)N)r   
__module____qualname__r:   propertyr@   r?   r)   rG   rH   r   r   r   rI   rJ   r   )r>   r   r   r>      s   

r>   r   )r9   r   )r>   r   r      s    8r   c                   s    e Zd ZdZ fddZ  ZS )r$   	levelnamer9   r   rC   c                sN   t  j| d|jddd}x&| jD ]}t||d }|r$|||< q$W tj|S )NZlog_messager   ZCRITICAL)typeZtimer   rN   )superr   ZcreatedRECORD_ATTRIBUTESgetattrr   dumps)r8   recorddataattrvalue)	__class__r   r   r      s    zJsonFormatter.format)rN   r9   r   rC   )r   rK   rL   rQ   r   __classcell__r   r   )rX   r   r$      s
      r$   )NN)NNr   r   FF)N)__doc__r0   r   r   Zlogging.configZlogging.handlersr   r    r   r   r/   r5   r   r%   r$   r   r   r   r   <module>   s   
	
?
K