o
    /wi(                     @  s4  U d dl m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
Z
d dlZd dlZd dlZg d
Ze Zded< daded< d2ddZd3ddZd4ddZd5ddZd6ddZd6dd Zd7d"d#Zd8d%d&Zd9d(d)Zd6d*d+Zd6d,d-Zd6d.d/Zd6d0d1ZdS ):    )annotationsN)CRITICAL)DEBUG)ERROR)FATAL)INFO)WARN)WARNING)r   r   r   r   r   r   r	   zthreading.Lock_lockzlogging.Handler | None_default_handlerreturnlogging.Formatterc                  C  s8   d} d}t  rtd|  d| S t|  d| S )z}Create a default formatter of log messages.

    This function is not supposed to be directly accessed by library users.
    z[%(levelname)1.1s %(asctime)s]z%(message)sz%(log_color)sz
%(reset)s  )_color_supportedcolorlogColoredFormatterlogging	Formatter)headermessage r   K/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/optuna/logging.pycreate_default_formatter    s   r   boolc                   C  s0   t jddr	dS ttjdrtj sdS dS )zDetection of color support.NO_COLORNFisattyT)osenvirongethasattrsysstderrr   r   r   r   r   r   .   s
   r   strc                   C  s   t dd S )N.r   )__name__splitr   r   r   r   _get_library_name:   s   r&   logging.Loggerc                   C  s   t t S N)r   	getLoggerr&   r   r   r   r   _get_library_root_logger>   s   r*   Nonec                  C  sv   t / tr	 W d    d S t att  t } | t | tj	 d| _
W d    d S 1 s4w   Y  d S )NF)r
   r   r   StreamHandlersetFormatterr   r*   
addHandlersetLevelr   	propagatelibrary_root_loggerr   r   r   _configure_library_root_loggerB   s   
"r3   c                  C  s`   t $ ts	 W d    d S t } | t | tj d aW d    d S 1 s)w   Y  d S r(   )r
   r   r*   removeHandlerr/   r   NOTSETr1   r   r   r   _reset_library_root_loggerS   s   
"r6   namec                 C  s   t   t| S )zzReturn a logger with the specified name.

    This function is not supposed to be directly accessed by library users.
    )r3   r   r)   )r7   r   r   r   
get_logger`   s   
r8   intc                   C  s   t   t  S )a  Return the current level for the Optuna's root logger.

    Example:

        Get the default verbosity level.

        .. testsetup::

            def objective(trial):
                x = trial.suggest_float("x", -100, 100)
                y = trial.suggest_categorical("y", [-1, 0, 1])
                return x**2 + y

        .. testcode::

            import optuna

            # The default verbosity level of Optuna is `optuna.logging.INFO`.
            print(optuna.logging.get_verbosity())
            # 20
            print(optuna.logging.INFO)
            # 20

            # There are logs of the INFO level.
            study = optuna.create_study()
            study.optimize(objective, n_trials=5)
            # [I 2021-10-31 05:35:17,232] A new study created ...
            # [I 2021-10-31 05:35:17,238] Trial 0 finished with value: ...
            # [I 2021-10-31 05:35:17,245] Trial 1 finished with value: ...
            # ...

        .. testoutput::
           :hide:

           20
           20
    Returns:
        Logging level, e.g., ``optuna.logging.DEBUG`` and ``optuna.logging.INFO``.

    .. note::
        Optuna has following logging levels:

        - ``optuna.logging.CRITICAL``, ``optuna.logging.FATAL``
        - ``optuna.logging.ERROR``
        - ``optuna.logging.WARNING``, ``optuna.logging.WARN``
        - ``optuna.logging.INFO``
        - ``optuna.logging.DEBUG``
    )r3   r*   getEffectiveLevelr   r   r   r   get_verbosityj   s   2
r;   	verbosityc                 C  s   t   t |  dS )aa  Set the level for the Optuna's root logger.

    Example:

        Set the logging level ``optuna.logging.WARNING``.

        .. testsetup::

            def objective(trial):
                x = trial.suggest_int("x", -10, 10)
                return x**2

        .. testcode::

            import optuna

            # There are INFO level logs.
            study = optuna.create_study()
            study.optimize(objective, n_trials=10)
            # [I 2021-10-31 02:59:35,088] Trial 0 finished with value: 16.0 ...
            # [I 2021-10-31 02:59:35,091] Trial 1 finished with value: 1.0 ...
            # [I 2021-10-31 02:59:35,096] Trial 2 finished with value: 1.0 ...

            # Setting the logging level WARNING, the INFO logs are suppressed.
            optuna.logging.set_verbosity(optuna.logging.WARNING)
            study.optimize(objective, n_trials=10)

        .. testcleanup::

            optuna.logging.set_verbosity(optuna.logging.INFO)


    Args:
        verbosity:
            Logging level, e.g., ``optuna.logging.DEBUG`` and ``optuna.logging.INFO``.

    .. note::
        Optuna has following logging levels:

        - ``optuna.logging.CRITICAL``, ``optuna.logging.FATAL``
        - ``optuna.logging.ERROR``
        - ``optuna.logging.WARNING``, ``optuna.logging.WARN``
        - ``optuna.logging.INFO``
        - ``optuna.logging.DEBUG``
    N)r3   r*   r/   )r<   r   r   r   set_verbosity   s   /r=   c                   C  "   t   tdus	J t t dS )a  Disable the default handler of the Optuna's root logger.

    Example:

        Stop and then resume logging to :obj:`sys.stderr`.

        .. testsetup::

            def objective(trial):
                x = trial.suggest_float("x", -100, 100)
                y = trial.suggest_categorical("y", [-1, 0, 1])
                return x**2 + y

        .. testcode::

            import optuna

            study = optuna.create_study()

            # There are no logs in sys.stderr.
            optuna.logging.disable_default_handler()
            study.optimize(objective, n_trials=10)

            # There are logs in sys.stderr.
            optuna.logging.enable_default_handler()
            study.optimize(objective, n_trials=10)
            # [I 2020-02-23 17:00:54,314] Trial 10 finished with value: ...
            # [I 2020-02-23 17:00:54,356] Trial 11 finished with value: ...
            # ...

    N)r3   r   r*   r4   r   r   r   r   disable_default_handler   s   !r?   c                   C  r>   )zEnable the default handler of the Optuna's root logger.

    Please refer to the example shown in :func:`~optuna.logging.disable_default_handler()`.
    N)r3   r   r*   r.   r   r   r   r   enable_default_handler   s   r@   c                   C     t   dt _dS )a  Disable propagation of the library log outputs.

    Note that log propagation is disabled by default. You only need to use this function
    to stop log propagation when you use :func:`~optuna.logging.enable_propagation()`.

    Example:

        Stop propagating logs to the root logger on the second optimize call.

        .. testsetup::

            def objective(trial):
                x = trial.suggest_float("x", -100, 100)
                y = trial.suggest_categorical("y", [-1, 0, 1])
                return x**2 + y

        .. testcode::

            import optuna
            import logging

            optuna.logging.disable_default_handler()  # Disable the default handler.
            logger = logging.getLogger()

            logger.setLevel(logging.INFO)  # Setup the root logger.
            logger.addHandler(logging.FileHandler("foo.log", mode="w"))

            optuna.logging.enable_propagation()  # Propagate logs to the root logger.

            study = optuna.create_study()

            logger.info("Logs from first optimize call")  # The logs are saved in the logs file.
            study.optimize(objective, n_trials=10)

            optuna.logging.disable_propagation()  # Stop propogating logs to the root logger.

            logger.info("Logs from second optimize call")
            # The new logs for second optimize call are not saved.
            study.optimize(objective, n_trials=10)

            with open("foo.log") as f:
                assert f.readline().startswith("A new study created")
                assert f.readline() == "Logs from first optimize call\n"
                # Check for logs after second optimize call.
                assert f.read().split("Logs from second optimize call\n")[-1] == ""

    FNr3   r*   r0   r   r   r   r   disable_propagation  s   1rC   c                   C  rA   )a  Enable propagation of the library log outputs.

    Please disable the Optuna's default handler to prevent double logging if the root logger has
    been configured.

    Example:

        Propagate all log output to the root logger in order to save them to the file.

        .. testsetup::

            def objective(trial):
                x = trial.suggest_float("x", -100, 100)
                y = trial.suggest_categorical("y", [-1, 0, 1])
                return x**2 + y

        .. testcode::

            import optuna
            import logging

            logger = logging.getLogger()

            logger.setLevel(logging.INFO)  # Setup the root logger.
            logger.addHandler(logging.FileHandler("foo.log", mode="w"))

            optuna.logging.enable_propagation()  # Propagate logs to the root logger.
            optuna.logging.disable_default_handler()  # Stop showing logs in sys.stderr.

            study = optuna.create_study()

            logger.info("Start optimization.")
            study.optimize(objective, n_trials=10)

            with open("foo.log") as f:
                assert f.readline().startswith("A new study created")
                assert f.readline() == "Start optimization.\n"

    TNrB   r   r   r   r   enable_propagation;  s   )rD   )r   r   )r   r   )r   r"   )r   r'   )r   r+   )r7   r"   r   r'   )r   r9   )r<   r9   r   r+   ) 
__future__r   r   r   r   r   r   r   r   r	   r   r    	threadingr   __all__Lockr
   __annotations__r   r   r   r&   r*   r3   r6   r8   r;   r=   r?   r@   rC   rD   r   r   r   r   <module>   s:    










6
3
'
5