
    j                        d dl mZ d dlZd dlZd dlmZmZmZm	Z	m
Z
mZmZmZmZ d dlmZ ddgZ G d de
          Z G d	 d
e	          Z G d d          ZeeddddddZdS )    )annotationsN)	CRITICALDEBUGERRORFilter	Formatter	getLoggerINFOStreamHandlerWARNING)stderrWatcherwatchc                  "     e Zd ZdZ fdZ xZS )ColourFormatterz- Colour formatter for pretty log output.
    c                   t                                          |          }|j        t          k    rd|z  S |j        t          k    rd|z  S |j        t
          k    rd|z  S |j        t          k    rd|z  S |j        t          k    rd|z  S |S )Nz[31;1m%s[0mz[33;1m%s[0mz[33m%s[0mz[37m%s[0mz[36m%s[0m)superformatlevelnor   r   r   r
   r   )selfrecords	__class__s      U/lsinfo/ai/hellotax_ai/base_platform/venv/lib/python3.11/site-packages/neo4j/debug.pyr   zColourFormatter.format-   s    GGNN6"">X%%(1,,^u$$(1,,^w&&&**^t##&**^u$$&**H    )__name__
__module____qualname____doc__r   __classcell__r   s   @r   r   r   )   sB                 r   r   c                      e Zd ZdZd ZdS )TaskIdFilterz)Injecting async task id into log records.c                    	 t          t          j                              |_        n# t          $ r
 d |_        Y nw xY wdS )NT)idasynciocurrent_tasktaskRuntimeError)r   r   s     r   filterzTaskIdFilter.filter@   sL    	W13344FKK 	 	 	FKKK	ts   %( <<N)r   r   r   r   r*    r   r   r#   r#   =   s)        33    r   r#   c                  T     e Zd ZdZeeddddd fdZddZd Z	 dddZ	ddZ
 xZS ) r   a5  Log watcher for easier logging setup.

    Example::

        from neo4j.debug import Watcher

        with Watcher("neo4j"):
            # DEBUG logging to stderr enabled within this context
            ...  # do something

    .. note:: The Watcher class is not thread-safe. Having Watchers in multiple
        threads can lead to duplicate log messages as the context manager will
        enable logging for all threads.

    .. note::
        The exact logging format and messages are not part of the API contract
        and might change at any time without notice. They are meant for
        debugging purposes and human consumption only.

    :param logger_names: Names of loggers to watch.
    :param default_level: Default minimum log level to show.
        The level can be overridden by setting ``level`` when calling
        :meth:`.watch`.
    :param default_out: Default output stream for all loggers.
        The level can be overridden by setting ``out`` when calling
        :meth:`.watch`.
    :type default_out: stream or file-like object
    :param colour: Whether the log levels should be indicated with ANSI colour
        codes.
    :param thread_info: whether to include information about the current
        thread in the log message. Defaults to :const:`True`.
    :param task_info: whether to include information about the current
        async task in the log message. Defaults to :const:`True`.

    .. versionchanged:: 5.3

        * Added ``thread_info`` and ``task_info`` parameters.
        * Logging format around thread and task information changed.
    FTdefault_leveldefault_outcolourthread_info	task_infologger_namest.Optional[str]r.   intr/   t.TextIOr0   boolr1   r2   returnNonec               6   t          t          |                                            || _        d | j        D             | _        || _        || _        i | _        || _        d}|rd|z   }|rd|z   }|sd|z   }|rt          nt          } ||          | _        d S )Nc                ,    g | ]}t          |          S r+   )r	   ).0names     r   
<listcomp>z$Watcher.__init__.<locals>.<listcomp>|   s    GGGT4GGGr   z%(asctime)s  %(message)sz[Task %(task)-15s] z[Thread %(thread)d] z[%(levelname)-8s] )r   r   __init__r3   _loggersr.   r/   	_handlers
_task_infor   r   	formatter)
r   r.   r/   r0   r1   r2   r3   format_formatter_clsr   s
            r   r?   zWatcher.__init__q   s     	gt%%'''(GGT5FGGG*&57#, 	6+g5G 	7,w6G 	5*W4G+1@y&w//r   c                .    |                                   | S )zEnable logging for all loggers.)r   )r   s    r   	__enter__zWatcher.__enter__   s    

r   c                .    |                                   dS z Disable logging for all loggers.N)stop)r   exc_typeexc_valexc_tbs       r   __exit__zWatcher.__exit__   s    		r   Nlevelt.Optional[int]outt.Optional[t.TextIO]c                   || j         }|| j        }|                                  t          |          }|                    | j                   |                    |           | j        r!|                    t                                 | j
        D ]S}|| j        |j        <   |                    |           |                                |k    r|                    |           TdS )a1  Enable logging for all loggers.

        :param level: Minimum log level to show.
            If :data:`None`, the ``default_level`` is used.
        :param out: Output stream for all loggers.
            If :data:`None`, the ``default_out`` is used.
        :type out: stream or file-like object
        N)r.   r/   rJ   r   setFormatterrC   setLevelrB   	addFilterr#   r@   rA   r=   
addHandlergetEffectiveLevel)r   rO   rQ   handlerloggers        r   r   zWatcher.watch   s     =&E;"C		$$T^,,,? 	.lnn---n 	' 	'F*1DN6;'g&&&''))E11&&&		' 	'r   c                    | j         D ]E}	 |                    | j                            |j                             6# t
          $ r Y Bw xY wdS rI   )r@   removeHandlerrA   popr=   KeyError)r   rZ   s     r   rJ   zWatcher.stop   sk    m 	 	F$$T^%7%7%D%DEEEE   	 	s   2>
A
A)r3   r4   r.   r5   r/   r6   r0   r7   r1   r7   r2   r7   r8   r9   )r8   r   )NN)rO   rP   rQ   rR   r8   r9   )r8   r9   )r   r   r   r   r   r   r?   rG   rN   r   rJ   r    r!   s   @r   r   r   H   s        & &V # & 0 0 0 0 0 0 0 06   
  
 JN' ' ' ' '6       r   FT)rO   rQ   r0   r1   r2   r3   r4   rO   r5   rQ   r6   r0   r7   r1   r2   r8   c           	     N    t          || ||||d}|                                 |S )ah  Quick wrapper for using  :class:`.Watcher`.

    Create a Watcher with the given configuration, enable watching and return
    it.

    Example::

        from neo4j.debug import watch

        watch("neo4j")
        # from now on, DEBUG logging to stderr is enabled in the driver

    .. note::
        The exact logging format and messages are not part of the API contract
        and might change at any time without notice. They are meant for
        debugging purposes and human consumption only.

    :param logger_names: Names of loggers to watch.
    :param level: see ``default_level`` of :class:`.Watcher`.
    :param out: see ``default_out`` of :class:`.Watcher`.
    :type out: stream or file-like object
    :param colour: see ``colour`` of :class:`.Watcher`.
    :param thread_info: see ``thread_info`` of :class:`.Watcher`.
    :param task_info: see ``task_info`` of :class:`.Watcher`.

    :returns: Watcher instance
    :rtype: :class:`.Watcher`

    .. versionchanged:: 5.3

        * Added ``thread_info`` and ``task_info`` parameters.
        * Logging format around thread and task information changed.
    r-   )r   r   )rO   rQ   r0   r1   r2   r3   watchers          r   r   r      s;    R |5c# )+ + +G MMOOONr   )r3   r4   rO   r5   rQ   r6   r0   r7   r1   r7   r2   r7   r8   r   )
__future__r   r&   typingtloggingr   r   r   r   r   r	   r
   r   r   sysr   __all__r   r#   r   r   r+   r   r   <module>rg      s}  " # " " " " "     
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
           i   (    6   n n n n n n n nf - - - - - - - -r   