o
    }oin'                     @  s"  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g d
Ze Zded< daded< d/ddZd0ddZd1ddZd2ddZd2ddZd3dd Zd4d"d#Zd5d%d&Zd2d'd(Zd2d)d*Zd2d+d,Zd2d-d.ZdS )6    )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  s$   d} d}t jd|  d| tj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 )stream)colorlogTTYColoredFormattersysstderr)headermessage r   B/home/ubuntu/.local/lib/python3.10/site-packages/optuna/logging.pycreate_default_formatter   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)logging	getLoggerr   r   r   r   r   _get_library_root_logger0   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_logger4   s   
"r*   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   NOTSETr(   r   r   r   _reset_library_root_loggerE   s   
"r-   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.
    )r*   r   r    )r.   r   r   r   
get_loggerR   s   
r/   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``
    )r*   r!   getEffectiveLevelr   r   r   r   get_verbosity\   s   2
r2   	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)r*   r!   r&   )r3   r   r   r   set_verbosity   s   /r4   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)r*   r   r!   r+   r   r   r   r   disable_default_handler   s   !r6   c                   C  r5   )zEnable the default handler of the Optuna's root logger.

    Please refer to the example shown in :func:`~optuna.logging.disable_default_handler()`.
    N)r*   r   r!   r%   r   r   r   r   enable_default_handler   s   r7   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r*   r!   r'   r   r   r   r   disable_propagation   s   1r:   c                   C  r8   )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"

    TNr9   r   r   r   r   enable_propagation-  s   )r;   )r   r   )r   r   )r   r   )r   r"   )r.   r   r   r   )r   r0   )r3   r0   r   r"   )
__future__r   r   r   r   r   r   r   r   r	   r   	threadingr   __all__Lockr
   __annotations__r   r   r   r!   r*   r-   r/   r2   r4   r6   r7   r:   r;   r   r   r   r   <module>   s6    









6
3
'
5