File manager - Edit - /opt/saltstack/salt/lib/python3.10/site-packages/cherrypy/__pycache__/_cplogging.cpython-310.pyc
Back
o ;jb@ � @ s� d Z ddlZddlZddlZddlZddlZddlmZ dejj_ e� d�ZG dd� dej�Z G dd � d e�ZG d d� dej�ZG dd � d e�ZdS )a� Simple config ============= Although CherryPy uses the :mod:`Python logging module <logging>`, it does so behind the scenes so that simple logging is simple, but complicated logging is still possible. "Simple" logging means that you can log to the screen (i.e. console/stdout) or to a file, and that you can easily have separate error and access log files. Here are the simplified logging settings. You use these by adding lines to your config file or dict. You should set these at either the global level or per application (see next), but generally not both. * ``log.screen``: Set this to True to have both "error" and "access" messages printed to stdout. * ``log.access_file``: Set this to an absolute filename where you want "access" messages written. * ``log.error_file``: Set this to an absolute filename where you want "error" messages written. Many events are automatically logged; to log your own application events, call :func:`cherrypy.log`. Architecture ============ Separate scopes --------------- CherryPy provides log managers at both the global and application layers. This means you can have one set of logging rules for your entire site, and another set of rules specific to each application. The global log manager is found at :func:`cherrypy.log`, and the log manager for each application is found at :attr:`app.log<cherrypy._cptree.Application.log>`. If you're inside a request, the latter is reachable from ``cherrypy.request.app.log``; if you're outside a request, you'll have to obtain a reference to the ``app``: either the return value of :func:`tree.mount()<cherrypy._cptree.Tree.mount>` or, if you used :func:`quickstart()<cherrypy.quickstart>` instead, via ``cherrypy.tree.apps['/']``. By default, the global logs are named "cherrypy.error" and "cherrypy.access", and the application logs are named "cherrypy.error.2378745" and "cherrypy.access.2378745" (the number is the id of the Application object). This means that the application logs "bubble up" to the site logs, so if your application has no log handlers, the site-level handlers will still log the messages. Errors vs. Access ----------------- Each log manager handles both "access" messages (one per HTTP request) and "error" messages (everything else). Note that the "error" log is not just for errors! The format of access messages is highly formalized, but the error log isn't--it receives messages from a variety of sources (including full error tracebacks, if enabled). If you are logging the access log and error log to the same source, then there is a possibility that a specially crafted error message may replicate an access log message as described in CWE-117. In this case it is the application developer's responsibility to manually escape data before using CherryPy's log() functionality, or they may create an application that is vulnerable to CWE-117. This would be achieved by using a custom handler escape any special characters, and attached as described below. Custom Handlers =============== The simple settings above work by manipulating Python's standard :mod:`logging` module. So when you need something more complex, the full power of the standard module is yours to exploit. You can borrow or create custom handlers, formats, filters, and much more. Here's an example that skips the standard FileHandler and uses a RotatingFileHandler instead: :: #python log = app.log # Remove the default FileHandlers if present. log.error_file = "" log.access_file = "" maxBytes = getattr(log, "rot_maxBytes", 10000000) backupCount = getattr(log, "rot_backupCount", 1000) # Make a new RotatingFileHandler for the error log. fname = getattr(log, "rot_error_file", "error.log") h = handlers.RotatingFileHandler(fname, 'a', maxBytes, backupCount) h.setLevel(DEBUG) h.setFormatter(_cplogging.logfmt) log.error_log.addHandler(h) # Make a new RotatingFileHandler for the access log. fname = getattr(log, "rot_access_file", "access.log") h = handlers.RotatingFileHandler(fname, 'a', maxBytes, backupCount) h.setLevel(DEBUG) h.setFormatter(_cplogging.logfmt) log.access_log.addHandler(h) The ``rot_*`` attributes are pulled straight from the application log object. Since "log.*" config entries simply set attributes on the log object, you can add custom attributes to your heart's content. Note that these handlers are used ''instead'' of the default, simple handlers outlined above (so don't set the "log.error_file" config entry, for example). � N)�_cperror� z%(message)sc @ s( e Zd ZdZdd� Zdd� Zdd� ZdS ) �NullHandlerzBA no-op logging handler to silence the logging.lastResort handler.c C � d S �N� ��self�recordr r �G/opt/saltstack/salt/lib/python3.10/site-packages/cherrypy/_cplogging.py�handle� � zNullHandler.handlec C r r r r r r r �emit� r zNullHandler.emitc C s d | _ d S r )�lock�r r r r � createLock� s zNullHandler.createLockN)�__name__� __module__�__qualname__�__doc__r r r r r r r r } s r c @ s� e Zd ZdZdZ dZ dZ dZdZ d)dd�Z dd� Z d d ejd fdd�Z d d� Zdd� Zdd� Zdd� Zd*dd�Zedd� �Zejdd� �Zdd� Zdd� Zedd� �Zejd d� �Zed!d"� �Zejd#d"� �Zd$d%� Zed&d'� �Zejd(d'� �ZdS )+� LogManagerznAn object to assist both simple and advanced logging. ``cherrypy.log`` is an instance of this class. Nz){h} {l} {u} {t} "{r}" {s} {b} "{f}" "{a}"�cherrypyc C s� || _ || _|d u rt�d| �| _t�d| �| _nt�d||f �| _t�d||f �| _| j�tj� | j�tj� | j�t � � | j�t � � t j�d| j � d S )Nz%s.errorz %s.accessz%s.error.%sz%s.access.%sZgraceful)�logger_root�appid�logging� getLogger� error_log� access_log�setLevel�INFO� addHandlerr r ZengineZ subscribe�reopen_files)r r r r r r �__init__� s � �zLogManager.__init__c C sX | j | jfD ]#}|jD ]}t|tj�r(|�� |j�� t |j |j�|_|�� qqdS )z#Close and reopen all file handlers.N) r r �handlers� isinstancer �FileHandler�acquire�stream�close�open�baseFilename�mode�release)r �log�hr r r r! � s ���zLogManager.reopen_files� Fc C s6 d}|rt �� }| jj|d�| �� ||f�|d� dS )a( Write the given ``msg`` to the error log. This is not just for errors! Applications may call this at any time to log application-specific information. If ``traceback`` is True, the traceback of the current exception (if any) will be appended to ``msg``. N� )�exc_info)r Z _exc_infor r- �join�time)r �msg�contextZseverity� tracebackr1 r r r �error� s �zLogManager.errorc O s | j |i |��S )zAn alias for ``error``.)r7 )r �args�kwargsr r r �__call__� s zLogManager.__call__c C sT t jj}|j}t jj}|j}|j}|jdu rd}n|j�dd�d }|�d�}|j p,|j dt|dd�p4d| �� |j |t�|dd �pBdt�|d d �t�|dd �t�|dd�|jt� d �}|�� D ](\}} t| t�slt| �} | �dd��d�} t| �dd� } | �dd�} | ||<