o
    
"«jd  ã                   @   s�   d Z ddlZddlZddlZddlmZ ddlmZ ddlZddl	m
Z
 daG dd„ dejƒZd	ed
efdd„Zdd„ Zdedejfdd„ZdS )aÇ  
logger_setup.py

Central logging configuration for this project (trade_bot). Every
module calls get_logger(__name__) to get a consistently formatted
logger that writes to both console and a log file under logs/.

Deliberately its own logger namespace ("trade_bot", not "sip_bot") so
this project's logs can never be mixed up with or accidentally routed
into screener/premarket's logging even if both processes run on the
same host at the same time.

Log files are one dated file per trading day (logs/{filename_prefix}_
YYYY-MM-DD.log), the day boundary anchored to market_time's America/
New_York clock rather than naive server-local time. Old daily files
beyond logging.retention_days are pruned on startup.
é    N)Údate)ÚPath)Ú
get_configFc                       sf   e Zd Zddededef‡ fdd„Zdedefd	d
„Zdd„ Z‡ fdd„Z	dd„ Z
‡ fdd„Z‡  ZS )Ú_DailyFileHandlerúutf-8Úlog_dirÚprefixÚencodingc                    s,   t ƒ  ¡  || _|| _|| _d | _d | _d S ©N)ÚsuperÚ__init__Ú_log_dirÚ_prefixÚ	_encodingÚ_current_dateÚ_file_handler)Úselfr   r   r	   ©Ú	__class__© ú'/var/www/screener/trade/logger_setup.pyr       s   

z_DailyFileHandler.__init__ÚdayÚreturnc                 C   s   | j | j› d| ¡ › d� S )NÚ_ú.log)r   r   Ú	isoformat)r   r   r   r   r   Ú	_path_for(   s   z_DailyFileHandler._path_forc                 C   sj   t  ¡  ¡ }| jd ur| j|krd S | jd ur| j ¡  || _tj|  |¡d| j	d�| _| j 
| j¡ d S )NÚa)Úmoder	   )Úmarket_timeÚnow_etr   r   r   ÚcloseÚloggingÚFileHandlerr   r   ÚsetFormatterÚ	formatter)r   Útodayr   r   r   Ú_ensure_current_file+   s   

ÿz&_DailyFileHandler._ensure_current_filec                    s*   t ƒ  |¡ | jd ur| j |¡ d S d S r
   )r   r$   r   )r   Úfmtr   r   r   r$   7   s   
ÿz_DailyFileHandler.setFormatterc                 C   s:   z|   ¡  | j |¡ W d S  ty   |  |¡ Y d S w r
   )r'   r   ÚemitÚ	ExceptionÚhandleError)r   Úrecordr   r   r   r)   <   s   ÿz_DailyFileHandler.emitc                    s"   | j d ur
| j  ¡  tƒ  ¡  d S r
   )r   r!   r   )r   r   r   r   r!   C   s   

z_DailyFileHandler.close)r   )Ú__name__Ú
__module__Ú__qualname__r   Ústrr   r   r   r'   r$   r)   r!   Ú__classcell__r   r   r   r   r      s    r   r   r   c              	   C   s¸   |r|dkrd S t  ¡  ¡  ¡ | }t t| |› d� ƒ¡D ];}tj |¡t	|ƒd t	dƒ … }zt 
|¡}W n	 tyA   Y qw | ¡ |k rYzt |¡ W q tyX   Y qw qd S )Nr   z_*.logé   r   )r   r    r   Ú	toordinalÚglobr0   ÚosÚpathÚbasenameÚlenÚfromisoformatÚ
ValueErrorÚremoveÚOSError)r   r   Úretention_daysÚcutoff_ordinalr6   ÚstemÚ	file_dater   r   r   Ú_prune_old_logsI   s$   "ÿÿýúrA   c            	      C   sæ   t rd S tƒ } |  di ¡}ttƒ ¡ j| dd¡ }|jddd� tt	| dd¡ 
¡ t	jƒ}| dd	¡}t	 d	¡}| |¡ d
|_t	jddd�}t	 ¡ }| |¡ | |¡ t||ƒ}| |¡ | |¡ t||| dd¡ƒ da d S )Nr"   r   ÚlogsT)ÚparentsÚexist_okÚlevelÚINFOÚfilename_prefixÚ	trade_botFz1%(asctime)s [%(levelname)s] %(name)s: %(message)sz%Y-%m-%d %H:%M:%S)Údatefmtr=   é   )Ú_CONFIGUREDr   Úgetr   Ú__file__ÚresolveÚparentÚmkdirÚgetattrr"   ÚupperrF   Ú	getLoggerÚsetLevelÚ	propagateÚ	FormatterÚStreamHandlerr$   Ú
addHandlerr   rA   )	ÚcfgÚlog_cfgr   rE   r   Úrootr(   ÚconsoleÚfile_handlerr   r   r   Ú_configure_rootZ   s.   

þ




r^   Únamer   c                 C   s   t ƒ  t d| › �¡S )Nz
trade_bot.)r^   r"   rS   )r_   r   r   r   Ú
get_logger|   s   r`   )Ú__doc__r4   r"   r5   Údatetimer   Úpathlibr   r   Úconfig_loaderr   rK   ÚHandlerr   r0   rA   r^   ÚLoggerr`   r   r   r   r   Ú<module>   s    *"