
    5fK                        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dlZddl
mZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZ ej,                  Zej.                  Zej0                  Zej2                  Zej2                  Zej6                  Zej8                  Zej:                  Zej<                  Z ej@                  ed       dddddddZ!d,dZ"d Z# G d dejH                        Z% G d de%      Z&d Z' G d de(      Z)d Z*d Z+d Z,d Z-d-d d!d"Z.	 	 d.d#Z/d$ Z0d% Z1d& Z2d' Z3i Z4d( Z5d/d)Z6d* Z7d+ Z8y# e	$ r dZY ,w xY w)0a  OpenStack logging handler.

This module adds to logging functionality by adding the option to specify
a context object when calling the various log methods.  If the context object
is not specified, default formatting is used. Additionally, an instance uuid
may be passed as part of the log message, which is intended to make it easier
for admins to find messages related to a specific instance.

It also allows setting of formatting information through conf.

    N)cfg)eventletutils)importutils)units)_)_options)
formatters)handlersTRACEsmhdwmidnight)secondsminuteshoursdaysweekdayr   c                    | j                   }| j                  }|r|s|S |r"|r t        j                  j	                  ||      S |r;|xs t        j                         }t        j                  j	                  ||      dS y )Nz.log)log_filelog_dirospathjoinr
   _get_binary_name)confbinarylogfilelogdirs       ./usr/lib/python3/dist-packages/oslo_log/log.py_get_log_file_pathr#   J   si    mmG\\Fv6ww||FG,,6844677<<799    c               #      K   t        j                          t         j                  j                  } | j                  j                         D ]!  }t        |t         j                        r| # yw)zIterate on existing loggers.N)logging	getLoggerLoggermanager
loggerDictvalues
isinstancePlaceHolder)r)   loggers     r"   _iter_loggersr/   [   s\      


nn$$G$$++- fg112s   A0A2c                   N    e Zd Zej                  j
                  Zed        Zd Z	y)BaseLoggerAdapterc                 .    | j                   j                  S N)r.   r
   selfs    r"   r
   zBaseLoggerAdapter.handlerso   s    {{###r$   c                 :     | j                   t        |g|i | y r3   )logr   )r5   msgargskwargss       r"   tracezBaseLoggerAdapter.traces   s    -d-f-r$   N)
__name__
__module____qualname__r&   LoggerAdapterwarningwarnpropertyr
   r;    r$   r"   r1   r1   k   s*      ((D$ $.r$   r1   c                       e Zd ZdZd Zy)KeywordArgumentAdaptera  Logger adapter to add keyword arguments to log record's extra data

    Keywords passed to the log call are added to the "extra"
    dictionary passed to the underlying logger so they are emitted
    with the log message and available to the format string.

    Special keywords:

    extra
      An existing dictionary of extra values to be passed to the
      logger. If present, the dictionary is copied and extended.
    resource
      A dictionary-like object containing a ``name`` key or ``type``
       and ``id`` keys.

    c                 ^   i }|j                  | j                         d|v r |j                  |j                  d             t        |j	                               D ]  }|dk(  r	|j                  |      ||<    t        t        |j	                                     |d<   ||d<   |d   j                  dd       }|rp|j                  dd       s@|j                  dd       }|j                  dd       }|r|rd|z   d	z   |z   d
z   |d   d<   ||fS d|j                  dd      z   d
z   |d   d<   ||fS )Nextraexc_info
extra_keysresourcenametypeid[-z]  )updaterG   poplistkeyssortedget)r5   r8   r:   rG   rK   rJ   resource_typeresource_ids           r"   processzKeywordArgumentAdapter.process   sU    TZZ fLLG,- ' 	+Dz! **T*E$K	+ #6%**,#78l  w '?&&z48
 <<- !)VT :&ll46 [363F36479D4EGK4LF7OJ/ F{ 03X\\&"5M/M1506w
+ F{r$   N)r<   r=   r>   __doc__rY   rC   r$   r"   rE   rE   w   s    "<r$   rE   c                       fd}|S )Nc                 J    d| ||fi} t              j                  di | y )NrH   )zUnhandled error)r'   critical)exc_typevaluetbrG   product_names       r"   logging_excepthookz6_create_logging_excepthook.<locals>.logging_excepthook   s,    hr23(	,((DeDr$   rC   )ra   rb   s   ` r"   _create_logging_excepthookrc      s    E r$   c                   (    e Zd Z ed      Zd Zd Zy)LogConfigErrorz8Error loading logging config %(log_config)s: %(err_msg)sc                      || _         || _        y r3   
log_configerr_msg)r5   rh   ri   s      r"   __init__zLogConfigError.__init__   s    $r$   c                 ^    | j                   t        | j                  | j                        z  S )Nrg   )messagedictrh   ri   r4   s    r"   __str__zLogConfigError.__str__   s'    ||ddoo+/<<9 9 	9r$   N)r<   r=   r>   r   rl   rj   rn   rC   r$   r"   re   re      s    JKG9r$   re   c                    	 t        t        d      sdt        _        t        j                  j                  |       }t        j                  |k7  rit               D ]/  }|j                  t        j                         g |_
        d|_        1 t        j                  j                  | d       |t        _        y y # t        j                  t         t        j"                  t$        f$ r}t'        | t)        |            d }~ww xY w)Nold_timer      F)disable_existing_loggers)hasattr_load_log_configrp   r   r   getmtimer/   setLevelr&   NOTSETr
   	propagateconfig
fileConfigconfigparserErrorKeyErrorerrorRuntimeErrorre   str)log_config_appendnew_timer.   excs       r"   rt   rt      s    :'4()%77##$56$$0 (/ %/"$#$ % NN%%&7?D & F(0% 1 "((LA :.C99:s   B5B9 9-D &C;;D c                     d|v rt        | j                         d|v rdt        _        | j                  rt        | j                         yy)z7Reconfigures oslo.log according to the mutated options.)Ndebug)Nzlog-config-appendr   N)_refresh_root_levelr   rt   rp   r   )r   freshs     r"   _mutate_hookr      sE     %DJJ'"e+$%!//0 r$   c                    t        j                  d      }|j                  s#|j                  t        j                                | j                  t        j                         | j                  t        j                         | j                  t        j                         | j                  t        j                         t        j                  |        | j                  t               y)zERegister the command line and configuration options used by oslo.log.N)r&   r'   r
   
addHandlerStreamHandlerregister_cli_optsr   common_cli_optslogging_cli_optsregister_optsgeneric_log_optslog_optsr	   _store_global_confregister_mutate_hookr   )r   root_loggers     r"   register_optionsr      s    
 ##D)Kw446783348445x001x(()!!$'l+r$   c                     t        j                  d      rpddl} ddlm} | j
                  j                  t        _        t        j                  j                         t        _	        |j                  t        j                  _        yy)zProperly setup logging with eventlet on native threads.

    Workaround for: https://github.com/eventlet/eventlet/issues/432
    threadr   N)
pipe_mutex)r   is_monkey_patchedeventlet.green.threadingoslo_logr   green	threadingr&   RLock_lockpipe_createLockHandler
createLock)eventletr   s     r"   _fix_eventlet_loggingr     sW     &&x0''$NN44))//1%/%?%?" 1r$   T)fix_eventletc                    |r
t                | j                  rt        | j                         nt        | ||       t	        |      t
        _        y)z*Setup logging for the current application.N)r   r   rt   _setup_logging_from_confrc   sys
excepthook)r   ra   versionr   s       r"   setupr     s:    //0 |W=/=CNr$   c                     |%t        j                  t        j                  |       | &t        j                  t        j                  |        yy)zBSet default values for the configuration options used by oslo.log.N)default_log_levels)logging_context_format_string)r   set_defaultsr   r   )r   r   s     r"   r   r   '  sL     %1	3 %0*G	I 1r$   c                 N    t        j                  t        j                  |        y)a`  Provide an API for tempest to set the logging filename.

    .. warning:: Only Tempest should use this function.

    We don't want applications to set a default log file, so we don't
    want this in set_defaults(). Because tempest doesn't use a
    configuration file we don't have another convenient way to safely
    set the log file default.

    )r   N)r   r   r   r   )filenames    r"   tempest_set_log_filer   8  s     X..Br$   c                    dD cg c]  }t        t        |d       r| }}| j                         } | j                  d      sd| z   } | |vr-t	        t        d      dj                  d |D              z        t        t        |       S c c}w )N)LOG_KERNLOG_USERLOG_MAIL
LOG_DAEMONLOG_AUTH
LOG_SYSLOGLOG_LPRLOG_NEWSLOG_UUCPLOG_CRONLOG_AUTHPRIVLOG_FTP
LOG_LOCAL0
LOG_LOCAL1
LOG_LOCAL2
LOG_LOCAL3
LOG_LOCAL4
LOG_LOCAL5
LOG_LOCAL6
LOG_LOCAL7LOG_z"syslog facility must be one of: %sz, c              3   &   K   | ]	  }d |z    yw)z'%s'NrC   ).0facs     r"   	<genexpr>z!_find_facility.<locals>.<genexpr>Z  s      "?&) #)3, "?s   )getattrsyslogupper
startswith	TypeErrorr   r   )facilityfvalid_facilitiess      r"   _find_facilityr   F  s    55a #61d3  5 5 ~~Hv&H$''>?		 "?-="? ?? @ 	@ 68$$)5s   Bc                     t        d      j                  }| r |j                  t        j                         y|j                  t        j
                         y)zSet the level of the root logger.

    :param debug: If 'debug' is True, the level will be DEBUG.
     Otherwise the level will be INFO.
    N)r'   r.   rv   r&   DEBUGINFO)r   log_roots     r"   r   r   `  s:     %%H'--(',,'r$   c           	      
   t        d       j                  }t        |j                        D ]  }|j	                  |        t        |       }|rt| j                  r3t        j                         dk(  rddl	m
} |j                  } ||      }n$| j                  j                         dk(  r{t        j                  j                  }| j                   j                         }	t"        |	   }
|
dk(  r|
t%        | j&                        z   }
 |||
| j&                  | j(                        }n| j                  j                         dk(  rMt        j                  j*                  }| j,                  t.        j0                  z  } |||| j(                        }n"t        j                  j2                  } ||      }|j5                  |       | j6                  r%t        j8                         }|j5                  |       | j:                  rMt<        t?        d	      tA        | jB                        }t        jD                  |
      }|j5                  |       | jF                  r\t        j                         dk(  r1t        j                  jI                  |      }|j5                  |       nt?        tK        d            |sL| j6                  s@| j:                  s4t        j8                  tL        jN                        }|j5                  |       | jP                  r5tS        jT                  dt        jV                        }|j5                  |       | jX                  rMt<        t?        d	      tA        | jB                        }t        jZ                  |
      }|j5                  |       | j\                  }| j^                  s:|j                  D ]*  }|ja                  tc        jd                  ||||              , n6|j                  D ]'  }|ja                  tc        jf                  |             ) ti        | jj                         | jl                  D ]_  }|jo                  d      \  }}}t        j                   |      }d }	 tq        |      }||ju                  |       O|ju                  |       a | jv                  dk\  rH| jx                  dk\  r8ddl	m=} |j}                  | jv                  | jx                  | j~                         y y y # tr        $ r Y w xY w)NLinuxr   )watchersintervalr   )whenr   backupCountsize)maxBytesr   z(syslog is not available on this platform)r   Windowsz4Windows Event Log is not available on this platform.z6oslo_messaging.notify.log_handler.PublishErrorsHandler)projectr   datefmtry   )r   =rq   )
rate_limit)@r'   r.   rS   r
   removeHandlerr#   watch_log_fileplatformsystemr   r   FastWatchedFileHandlerlog_rotation_typelowerr&   TimedRotatingFileHandlerlog_rotate_interval_typeLOG_ROTATE_INTERVAL_MAPPINGr   log_rotate_intervalmax_logfile_countRotatingFileHandlermax_logfile_size_mbr   MiWatchedFileHandlerr   
use_stderrColorHandleruse_journalr   r   r   syslog_log_facilityOSJournalHandleruse_eventlogNTEventLogHandlerr   r   stdoutpublish_errorsr   import_objectERROR
use_syslogOSSysLogHandlerlog_date_formatuse_jsonsetFormatterr	   ContextFormatterJSONFormatterr   r   r   	partitionint
ValueErrorrv   rate_limit_burstrate_limit_intervalr   install_filterrate_limit_except)r   r   r   r   handlerlogpathr   file_handlerfilelogr   interval_typer   	streamlogr   journaleventlogsyslog_handlerr   pairmod_sep
level_namer.   numeric_levelr   s                            r"   r   r   m  sU   %%H ))* (w'( !&G8??#4#?)#::L"7+G##))+z9"++DDL00668D7=M # -D4L4L0M M"7(5,0,D,D/3/E/EGG ##))+v5"++??L//%((:H"7,4/3/E/EGG #++>>L"7+GG$))+	I&>IJJ!$":":;++X>G$??	)''99'BH)q "-  . / / 4??43C3C ))#**5	I&++DMM 	G$>IJJ!$":":;!118DN+""G==(( 	KG  !<!<WELELDH"J K	K  (( 	LG  !9!9'!JK	L

#'' ( $s 3T:""3'	
  
OM $OOM*OOJ'(" !d&>&>!&C'!!$"7"7"&":":"&"8"8	: 'D!  		s   6T  	TTc                  *    t         j                         S )z-Return a copy of the oslo loggers dictionary.)_loggerscopyrC   r$   r"   get_loggersr!    s    ==?r$   c                     | r| j                  d      rd| dd z   } | t        vr)t        t        j                  |       ||d      t        | <   t        |    S )a  Build a logger with the given name.

    :param name: The name for the logger. This is usually the module
                 name, ``__name__``.
    :type name: string
    :param project: The name of the project, to be injected into log
                    messages. For example, ``'nova'``.
    :type project: string
    :param version: The version of the project, to be injected into log
                    messages. For example, ``'2014.2'``.
    :type version: string
    oslo_zoslo.   N)r   r   )r   r  rE   r&   r'   )rK   r   r   s      r"   r'   r'     s^    $ (ab!8/0A0A$0G<C<C1EF D>r$   c                  4    t        t        j                        S )zReturn the Oslo Logging default log levels.

    Returns a copy of the list so an application can change the value
    and not affect the default value used in the log_opts configuration
    setup.
    )rS   r   DEFAULT_LOG_LEVELSrC   r$   r"   get_default_log_levelsr'    s     ++,,r$   c                     | j                   S )z+Determine if debug logging mode is enabled.)r   )r   s    r"   is_debug_enabledr)    s    ::r$   r3   )unknown)NN)Nr*  r*  )9rZ   r{   r&   logging.configlogging.handlersr   r   r   r   ImportErroroslo_configr   
oslo_utilsr   r   r   oslo_log._i18nr   r   r   r	   r
   CRITICALFATALr  WARNINGWARNr   r   rw   _TRACEr   addLevelNamer   r#   r/   r?   r1   rE   rc   	Exceptionre   rt   r   r   r   r   r   r   r   r   r   r  r!  r'   r'  r)  rC   r$   r"   <module>r8     sy  $
     	  
  $ "     
//||	   UG $  " 	.-- 	.N. Nb
9Y 
9:&	1,$@> > 04$(I"C%4
(n:b 
6-I  Fs   E EE