o
    -wi                     @  s   U d Z ddlmZ ddlZddlZddlZddlZddlmZ G dd dZ	e	 Z
ejdddZd	ed< ed
ZdddZejdddZejd ddZd!ddZG dd dejZdS )"a  Logging configuration for the "wandb" logger.

Most log statements in wandb are made in the context of a run and should be
redirected to that run's log file (usually named 'debug.log'). This module
provides a context manager to temporarily set the current run ID and registers
a global handler for the 'wandb' logger that sends log statements to the right
place.

All functions in this module are threadsafe.

NOTE: The pytest caplog fixture will fail to capture logs from the wandb logger
because they are not propagated to the root logger.
    )annotationsN)Iteratorc                   @  s   e Zd ZdZdS )_NotRunSpecificz"Sentinel for `not_run_specific()`.N)__name__
__module____qualname____doc__ r	   r	   U/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/wandb/sdk/lib/wb_logging.pyr      s    r   _run_id)defaultz4contextvars.ContextVar[str | _NotRunSpecific | None]wandbreturnNonec                   C  s.   t tj dt _t jst t  dS dS )a#  Configures the global 'wandb' logger.

    The wandb logger is not intended to be customized by users. Instead, it is
    used as a mechanism to redirect log messages into wandb run-specific log
    files.

    This function is idempotent: calling it multiple times has the same effect.
    FN)_loggersetLevelloggingDEBUG	propagatehandlers
addHandlerNullHandlerr	   r	   r	   r
   configure_wandb_logger'   s
   r   run_id
str | NoneIterator[None]c              	   c  s0    t | }zdV  W t | dS t | w )au  Direct all wandb log messages to the given run.

    Args:
        id: The current run ID, or None if actions in the context manager are
            not associated to a specific run. In the latter case, log messages
            will go to all runs.

    Usage:

        with wb_logging.run_id(...):
            ... # Log messages here go to the specified run's logger.
    N)r   setreset)r   tokenr	   r	   r
   
log_to_runH   s
   
r   c               	   c  s0    t t} zdV  W t |  dS t |  w )a)  Direct wandb log messages to all runs.

    Unlike `log_to_run(None)`, this indicates an intentional choice.
    This is often convenient to use as a decorator:

        @wb_logging.log_to_all_runs()
        def my_func():
            ... # Log messages here go to the specified run's logger.
    N)r   r   _NOT_RUN_SPECIFICr   )r   r	   r	   r
   log_to_all_runs]   s
   
r!   strfilepathpathlib.Pathlogging.Handlerc                 C  sB   t |}|t j |t|  |t d t	| |S )aS  Direct log messages for a run to a file.

    Args:
        run_id: The run for which to create a log file.
        filepath: The file to write log messages to.

    Returns:
        The added handler which can then be configured further or removed
        from the 'wandb' logger directly.

        The default logging level is INFO.
    z|%(asctime)s %(levelname)-7s %(threadName)-10s:%(process)d [%(filename)s:%(funcName)s():%(lineno)s]%(run_id_tag)s %(message)s)
r   FileHandlerr   INFO	addFilter_RunIDFiltersetFormatter	Formatterr   r   )r   r#   handlerr	   r	   r
   add_file_handlero   s   

r-   c                   @  s$   e Zd ZdZdddZdddZdS )r)   z0Filters out messages logged for a different run.r   r"   r   r   c                 C  s
   || _ dS )zsCreate a _RunIDFilter.

        Args:
            run_id: Allows messages when the run ID is this or None.
        N)r   )selfr   r	   r	   r
   __init__   s   
z_RunIDFilter.__init__recordlogging.LogRecordboolc                 C  s>   t  }|d u rd|_dS t|trd|_dS d|_|| j kS )Nz [no run ID]Tz [all runs] )r   get
run_id_tag
isinstancer   )r.   r0   r   r	   r	   r
   filter   s   

z_RunIDFilter.filterN)r   r"   r   r   )r0   r1   r   r2   )r   r   r   r   r/   r7   r	   r	   r	   r
   r)      s    
r)   )r   r   )r   r   r   r   )r   r   )r   r"   r#   r$   r   r%   )r   
__future__r   
contextlibcontextvarsr   pathlibtypingr   r   r    
ContextVarr   __annotations__	getLoggerr   r   contextmanagerr   r!   r-   Filterr)   r	   r	   r	   r
   <module>   s*    

!
