
    yf$                         S r SSKrSSKrSSKrSSKrSSKrSSKrSSKrSqS
S jr	SS jr
S rSS jr " S S	\R                  5      rg)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                 r    U SU SUR                    SU  3n[        [         5      nUR                  U5        g )N:z: )__name__create_loggerwarning)messagecategoryfilenamelinenofilelinemsgloggers           -/usr/lib/python3/dist-packages/borg/logger.py_log_warningr   /   s=    Jaxr("3"3!4Bwi
@C8$F NN3    c                    SnU(       a  [         R                  R                  X!5      nU(       a   [         R                  R	                  U5      n[        U5       n[        R                  R                  U5        SSS5        Sq	[        R                  " [        5      n[        R                  " S5      n	XYl        UR                  SU S35        [        [        l        g[        R                  " S5      n[        R&                  " U 5      nU(       a
  U(       d  SnOSnU(       a  [)        U5      O[        R*                  " U5      nUR-                  U5        [        R                  " S5      n	Xl        XYl        [        (       aH  UR0                  (       a7  UR0                  S	   R3                  5         UR0                  R5                  5         UR7                  U5        UR9                  UR;                  5       5        Sq	[        R                  " [        5      nU(       a  UR=                  S
U SU S35        UR                  S5        [        [        l        U$ ! , (       d  f       GN= f! ["         a  n
[%        U
5      n Sn
A
GNSn
A
ff = f)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.
NTborgz'using logging configuration read from "" z/$LOG %(levelname)s %(name)s Remote: %(message)sz%(message)sr   zsetup_logging for "z" failed with "z".z,using builtin fallback logging configuration)osenvirongetpathabspathopenloggingconfig
fileConfig
configured	getLoggerr   jsondebugr   warningsshowwarning	ExceptionstrStreamHandlerJsonFormatter	FormattersetFormatter	formatterhandlerscloseclear
addHandlersetLevelupperr   )stream
conf_fnameenv_varlevelis_server"   err_msgfr   borg_loggererrhandlerfmtr,   s                 r   setup_loggingr>   8   s    GZZ^^G8
	4J
 j!Q))!, "J&&x0F!++F3K#LLB:,aPQ#/H  r"F##F+G?&*c"0A0A#0FI###F+K%zfoo 	  "
g
OOEKKM"Jx(F,ZLyPRST
LL?@'HNK "!  	#hG	s0   *I,  I;A$I, 
I)$I, ,
J6JJc                  Z    [         R                  " 5       R                  n [         R                  " U 5      nUb  UR                  [        :X  a=  U R                  n [         R                  " U 5      nUc  M'  UR                  [        :X  a  M=  UR                  $ ! [
         a	    [        s $ f = f)zfind the name of the first module calling this module

if we cannot find it, we return the current module's name
(__name__) instead.
)inspectcurrentframef_back	getmoduler   AttributeError)framemodules     r   find_parent_modulerG   w   s    
$$&--""5)n8 ;LLE&&u-F n8 ;   s   A1B 5B B B*)B*c                 .   ^  " U4S jS5      mT" U 5      $ )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                   t   > \ rS rSrSS jr\S 5       rU 4S jrS rS r	SS	.S
 jr
S rS rS rS rS rSrg)!create_logger.<locals>.LazyLogger   Nc                 B    U=(       d
    [        5       U l        S U l        g N)rG   _LazyLogger__name_LazyLogger__real_logger)selfnames     r   __init__*create_logger.<locals>.LazyLogger.__init__   s    6"4"6DK!%Dr   c                 p   U R                   c  [        (       d  [        S5      e[        R                  " U R
                  5      U l         U R
                  R                  S5      (       aC  U R                   R                  [        R                  :X  a  U R                   R                  S5        U R                   $ )Nz8tried to call a logger before setup_logging() was calledzborg.debug.WARNING)
rO   r    r&   r   r!   rN   
startswithr6   NOTSETr1   )rP   s    r   __logger*create_logger.<locals>.LazyLogger.__logger   s    !!)!z#$^__%,%6%6t{{%C";;))-88T=O=O=U=UY`YgYg=g&&//	:%%%r   c                 4   > T" U R                   S-   U-   5      $ )N.)rN   )rP   suffix
LazyLoggers     r   getChild*create_logger.<locals>.LazyLogger.getChild   s    dkkC/&899r   c                 8    U R                   R                  U5      $ rM   )_LazyLogger__loggerr1   )rP   r6   s     r   r1   *create_logger.<locals>.LazyLogger.setLevel   s    ==))%00r   c                     SU;   a$  UR                  S5      UR                  S0 5      S'   U R                  R                  " X/UQ70 UD6$ Nmsgidextra)pop
setdefaultra   log)rP   r6   r   argskwargss        r   ri   %create_logger.<locals>.LazyLogger.log   sI    & :@**W:M!!'2.w7==$$UA$A&AAr   T)exc_infoc                    SU;   a$  UR                  S5      UR                  S0 5      S'   U R                  R                  " U/UQ7SU0UD6$ )Nre   rf   rm   )rg   rh   ra   	exception)rP   r   rm   rj   rk   s        r   ro   +create_logger.<locals>.LazyLogger.exception   sN    & :@**W:M!!'2.w7==**3SSSFSSr   c                     SU;   a$  UR                  S5      UR                  S0 5      S'   U R                  R                  " U/UQ70 UD6$ rd   )rg   rh   ra   r#   rP   r   rj   rk   s       r   r#   'create_logger.<locals>.LazyLogger.debug   I    & :@**W:M!!'2.w7==&&s<T<V<<r   c                     SU;   a$  UR                  S5      UR                  S0 5      S'   U R                  R                  " U/UQ70 UD6$ rd   )rg   rh   ra   inforr   s       r   rv   &create_logger.<locals>.LazyLogger.info   sI    & :@**W:M!!'2.w7==%%c;D;F;;r   c                     SU;   a$  UR                  S5      UR                  S0 5      S'   U R                  R                  " U/UQ70 UD6$ rd   )rg   rh   ra   r   rr   s       r   r   )create_logger.<locals>.LazyLogger.warning   sI    & :@**W:M!!'2.w7==((>t>v>>r   c                     SU;   a$  UR                  S5      UR                  S0 5      S'   U R                  R                  " U/UQ70 UD6$ rd   )rg   rh   ra   errorrr   s       r   r{   'create_logger.<locals>.LazyLogger.error   rt   r   c                     SU;   a$  UR                  S5      UR                  S0 5      S'   U R                  R                  " U/UQ70 UD6$ rd   )rg   rh   ra   criticalrr   s       r   r~   *create_logger.<locals>.LazyLogger.critical   sI    & :@**W:M!!'2.w7==))#????r   )__name__real_loggerrM   )r   
__module____qualname____firstlineno__rR   propertyra   r^   r1   ri   ro   r#   rv   r   r{   r~   __static_attributes__)r]   s   r   r]   rJ      sQ    	& 
	& 
	&	:	1	B
 26 	T
	=
	<
	?
	=
	@r   r]    )rQ   r]   s    @r   r   r      s     6@ 6@p dr   c                   ,   ^  \ rS rSrSrU 4S jrSrU =r$ )r)      )	levelnamerQ   r   re   c                    > [         TU ]  U5        SUR                  SSS.nU R                   H  n[	        XS 5      nU(       d  M  XBU'   M     [
        R                  " U5      $ )Nlog_messager   CRITICAL)typetimer   r   )superformatcreatedRECORD_ATTRIBUTESgetattrr"   dumps)rP   recorddataattrvalue	__class__s        r   r   JsonFormatter.format   sa    v!NN#	
 **DF$/Eu"T
 + zz$r   r   )r   r   r   r   r   r   r   __classcell__)r   s   @r   r)   r)      s        r   r)   )NN)NNBORG_LOGGING_CONFrv   FFrM   )__doc__r@   r"   r   logging.configlogging.handlersr   r$   r    r   r>   rG   r   r*   r)   r   r   r   <module>r      sP   @      	 
<~&HV G%%  r   