o
    bi+                     @   s  d dl Z d dlZ d dlZd dlmZmZ d dlZd dlZdZej	j
jjZdddeidej dej id	d
did
didddddddddgdddddgdddddgdddddgddddZdd iZd!Zd"Zi adae e jd d# G d$d% d%ZG d&d' d'e jZd(ee fd)d*Zd(ee fd+d,Zd>d-d.Zd>d/d0Z d(ee fd1d2Z!d(e j"fd3d4Z#d5ed(efd6d7Z$d5ee d(dfd8d9Z%d5ed(ee& fd:d;Z'd5ed(ee& fd<d=Z(dS )?    N)ListOptionalz@%(asctime)s	%(levelname)s %(filename)s:%(lineno)s -- %(message)s   Fformatclass.)rayray_jsonz()z-ray.data._internal.logging.HiddenRecordFilterz2ray._private.ray_logging.filters.CoreContextFilter)console_filtercore_context_filterz-ray.data._internal.logging.SessionFileHandlerr   zray-data.log)r   	formatterfilenamer	   r   )r   r   r   filtersz ray._private.log.PlainRayHandlerINFOr
   )r   r   levelr   )file	file_jsonconsoleDEBUGr   r   )r   handlers	propagate)ray.datazray.air.util.tensor_extensions)versiondisable_existing_loggers
formattersr   r   loggersr   RAY_DATA_LOG_ENCODINGRAY_DATA_LOGGING_CONFIGTRACEc                   @   s   e Zd ZdZdd ZdS )HiddenRecordFiltera  Filters out log records with the "hide" attribute set to True.

    This filter allows you to override default logging behavior. For example, if errors
    are printed by default, and you don't want to print a specific error, you can set
    the "hide" attribute to avoid printing the message.

    .. testcode::

        import logging
        logger = logging.getLogger("ray.data.spam")

        # This warning won't be printed to the console.
        logger.warning("ham", extra={"hide": True})
    c                 C   s   t |dd S )NhideF)getattrselfrecord r%   N/home/ubuntu/.local/lib/python3.10/site-packages/ray/data/_internal/logging.pyfilterf   s   zHiddenRecordFilter.filterN)__name__
__module____qualname____doc__r'   r%   r%   r%   r&   r   V   s    r   c                       sJ   e Zd ZdZdef fddZdd Zdejdd	fd
dZ	dd Z
  ZS )SessionFileHandleral  A handler that writes to a log file in the Ray session directory.

    The Ray session directory isn't available until Ray is initialized, so this handler
    lazily creates the file handler when you emit a log record.

    Args:
        filename: The name of the log file. The file is created in the 'logs' directory
            of the Ray session directory.
    r   c                    s&   t    || _d | _d | _d | _d S N)super__init__	_filename_handler
_formatter_path)r#   r   	__class__r%   r&   r/   u   s
   

zSessionFileHandler.__init__c                 C   s0   | j d u r	|   | j d ur| j | d S d S r-   )r1   _try_create_handleremitr"   r%   r%   r&   r7   |   s
   

zSessionFileHandler.emitfmtreturnNc                 C   s    | j d ur| j | || _d S r-   )r1   setFormatterr2   )r#   r8   r%   r%   r&   r:      s   

zSessionFileHandler.setFormatterc                 C   sn   | j d u sJ t }|d u rd S tj|dd tj|| j| _t	| j| _ | j
d ur5| j | j
 d S d S )NT)exist_ok)r1   get_log_directoryosmakedirspathjoinr0   r3   loggingFileHandlerr2   r:   )r#   log_directoryr%   r%   r&   r6      s   
z&SessionFileHandler._try_create_handler)r(   r)   r*   r+   strr/   r7   rA   	Formatterr:   r6   __classcell__r%   r%   r4   r&   r,   j   s    
r,   r9   c                  C   s   dt fdd} tjt}tjt}|d ur| |}|S t}|d urI| dkrI|d  D ]}t	
 D ]\}}|d | |d | q5q/|S )Nconfig_pathc                 S   s8   t | }t|}W d    |S 1 sw   Y  |S r-   )openyaml	safe_load)rG   r   configr%   r%   r&   _load_logging_config   s   

z1_get_logging_config.<locals>._load_logging_configJSONr   r   )rD   r=   environget$RAY_DATA_LOGGING_CONFIG_ENV_VAR_NAME"RAY_DATA_LOG_ENCODING_ENV_VAR_NAMEDEFAULT_CONFIGuppervalues'RAY_DATA_LOG_HANDLER_JSON_SUBSTITUTIONSitemsremoveappend)rL   rG   log_encodingrK   loggerold_handler_namenew_handler_namer%   r%   r&   _get_logging_config   s"   r]   c                  C   s   t  di } t|  S )Nr   )r]   rO   listkeys)logger_configr%   r%   r&   _get_logger_names   s   ra   c                  C   sZ   t jt} t jt}t }tj| | dur)|dur+t	t
}|d dS dS dS )a  Configure the Python logger named 'ray.data'.

    This function loads the configration YAML specified by "RAY_DATA_LOGGING_CONFIG"
    environment variable. If the variable isn't set, this function loads the default
    "logging.yaml" file that is adjacent to this module.

    If "RAY_DATA_LOG_ENCODING" is specified as "JSON" we will enable JSON logging mode
    if using the default logging config.
    NzMUsing `RAY_DATA_LOG_ENCODING` is not supported with `RAY_DATA_LOGGING_CONFIG`)r=   rN   rO   rP   rQ   r]   rA   rK   
dictConfig	getLoggerr(   warning)rG   rY   rK   rZ   r%   r%   r&   configure_logging   s   
re   c                  C   s,   t d} | j  | t j i adadS )zSReset the logger named 'ray.data' to its initial state.

    Used for testing.
    r   N)rA   rc   r   clearsetLevelNOTSET_DATASET_LOGGER_HANDLER_ACTIVE_DATASET)rZ   r%   r%   r&   reset_logging   s
   

rk   c                  C   s.   t jjj} | du rdS |  }tj|ddS )zyReturn the directory where Ray Data writes log files.

    If Ray isn't initialized, this function returns ``None``.
    Nlogszray-data)r   _privateworker_global_nodeget_session_dir_pathr=   r?   r@   )global_nodesession_dirr%   r%   r&   r<      s
   
r<   c                  C   s0   t jt} | d ur|  dkrt S ttS )NrM   )	r=   rN   rO   rQ   rS   DEFAULT_JSON_FORMATTERrA   rE   DEFAULT_TEXT_FORMATTER)rY   r%   r%   r&   _get_default_formatter   s   
ru   
dataset_idc                 C   s"   t d|  dd}|t  |S )zCreate a log handler for a dataset with the given ID.

    Args:
        dataset_id: The ID of the dataset.

    Returns:
        A log handler for the dataset.
    z	ray-data-z.log)r   )r,   r:   ru   )rv   handlerr%   r%   r&   _create_dataset_log_handler   s   	rx   c                 C   s>   | sdS t   t| }dd t D }|D ]}|| qdS )a<  Create a log handler for a dataset with the given ID. Switch the dataset logger
    for the worker to this dataset logger. Note that only the driver keeps track of the
    active dataset. The worker will just use the handler that the driver tells it to use.

    Args:
        dataset_id: The ID of the dataset.
    Nc                 S      g | ]}t |qS r%   rA   rc   .0namer%   r%   r&   
<listcomp>      z4update_dataset_logger_for_worker.<locals>.<listcomp>)re   rx   ra   
addHandler)rv   log_handlerr   rZ   r%   r%   r&    update_dataset_logger_for_worker  s   r   c                 C   s   dd t  D }t| }tt}|| |d|  |t| < ts/| a|D ]}|| q&n||  dt d|  d |	| tS )a  Create a log handler for a dataset with the given ID. Activate the handler if
    this is the only active dataset. Otherwise, print a warning to that handler and
    keep it inactive until it becomes the only active dataset.

    Args:
        dataset_id: The ID of the dataset.
    c                 S   ry   r%   rz   r{   r%   r%   r&   r~      r   z+register_dataset_logger.<locals>.<listcomp>z(Registered dataset logger for dataset %sz- registers for logging while another dataset zJ is also logging. For performance reasons, we will not log to the dataset z% until it is the only active dataset.)
ra   rx   rA   rc   r(   r   infori   rj   removeHandler)rv   r   r   local_loggerrZ   r%   r%   r&   register_dataset_logger  s(   



r   c                 C   sd   dd t  D }t| d}t| kr datr tttt  |r0|D ]}|| q$|	  tS )zmRemove the logger for a dataset with the given ID.

    Args:
        dataset_id: The ID of the dataset.
    c                 S   ry   r%   rz   r{   r%   r%   r&   r~   A  r   z-unregister_dataset_logger.<locals>.<listcomp>N)
ra   ri   poprj   r   nextiterr_   r   close)rv   r   r   rZ   r%   r%   r&   unregister_dataset_logger9  s   r   )r9   N))rA   logging.configr=   typingr   r   rI   r   rt   rm   ray_loggingr   JSONFormatterrs   r)   r(   rR   rU   rQ   rP   ri   rj   addLevelNamer   r   Handlerr,   dictr]   rD   ra   re   rk   r<   rE   ru   rx   r   intr   r   r%   r%   r%   r&   <module>   s|    2
,

#