a
    ù!f°@  ã                   @   s<   d Z ddlZddlmZ G dd„ deƒZG dd„ deƒZdS )zDefine API Loggers.é    N)ÚMessageToJsonc                   @   s˜   e Zd ZdZddd„Zedd„ ƒZedd„ ƒZed	d
„ ƒZedd„ ƒZ	dd„ Z
ddd„Zddd„Zd dd„Zd!dd„Zd"dd„Zd#dd„Zd$dd„ZdS )%ÚLoggera6  Loggers represent named targets for log entries.

    See:
    https://cloud.google.com/logging/docs/api/ref_v2beta1/rest/v2beta1/projects.logs

    :type name: string
    :param name: the name of the logger

    :type client: :class:`gcloud.logging.client.Client`
    :param client: A client which holds credentials and project configuration
                   for the logger (which requires a project).

    :type labels: dict or :class:`NoneType`
    :param labels: (optional) mapping of default labels for entries written
                   via this logger.
    Nc                 C   s   || _ || _|| _d S ©N)ÚnameÚ_clientÚlabels)Úselfr   Úclientr   © r
   úV/var/www/html/python-backend/venv/lib/python3.9/site-packages/gcloud/logging/logger.pyÚ__init__'   s    zLogger.__init__c                 C   s   | j S )zClent bound to the logger.©r   ©r   r
   r
   r   r	   ,   s    zLogger.clientc                 C   s   | j jS )zProject bound to the logger.)r   Úprojectr   r
   r
   r   r   1   s    zLogger.projectc                 C   s   d| j | jf S )z)Fully-qualified name used in logging APIszprojects/%s/logs/%s)r   r   r   r
   r
   r   Ú	full_name6   s    zLogger.full_namec                 C   s   d| j f S )z URI path for use in logging APIsz/%s)r   r   r
   r
   r   Úpath;   s    zLogger.pathc                 C   s   |du r| j }|S )a€  Check client or verify over-ride.

        :type client: :class:`gcloud.logging.client.Client` or ``NoneType``
        :param client: the client to use.  If not passed, falls back to the
                       ``client`` stored on the current logger.

        :rtype: :class:`gcloud.logging.client.Client`
        :returns: The client passed in or the currently bound client.
        Nr   ©r   r	   r
   r
   r   Ú_require_client@   s    
zLogger._require_clientc                 C   s   |   |¡}t| |ƒS )ac  Return a batch to use as a context manager.

        :type client: :class:`gcloud.logging.client.Client` or ``NoneType``
        :param client: the client to use.  If not passed, falls back to the
                       ``client`` stored on the current topic.

        :rtype: :class:`Batch`
        :returns: A batch to use as a context manager.
        )r   ÚBatchr   r
   r
   r   ÚbatchN   s    

zLogger.batchc                 C   s¤   | j ddidœ}|dur ||d< |dur0||d< |durRt|ƒ}	t |	¡}
|
|d< |du r`| j}|durp||d< |dur€||d	< |dur||d
< |dur ||d< |S )a/  Return a log entry resource of the appropriate type.

        Helper for :meth:`log_text`, :meth:`log_struct`, and :meth:`log_proto`.

        Only one of ``text``, ``info``, or ``message`` should be passed.

        :type text: string or :class:`NoneType`
        :param text: text payload

        :type info: dict or :class:`NoneType`
        :param info: struct payload

        :type message: Protobuf message or :class:`NoneType`
        :param message: protobuf payload

        :type labels: dict or :class:`NoneType`
        :param labels: labels passed in to calling method.

        :type insert_id: string or :class:`NoneType`
        :param insert_id: (optional) unique ID for log entry.

        :type severity: string or :class:`NoneType`
        :param severity: (optional) severity of event being logged.

        :type http_request: dict or :class:`NoneType`
        :param http_request: (optional) info about HTTP request associated with
                             the entry

        :rtype: dict
        :returns: The JSON resource created.
        ÚtypeÚglobal)ZlogNameÚresourceNÚtextPayloadÚjsonPayloadÚprotoPayloadr   ÚinsertIdÚseverityÚhttpRequest)r   r   ÚjsonÚloadsr   )r   ÚtextÚinfoÚmessager   Ú	insert_idr   Úhttp_requestr   Úas_json_strÚas_jsonr
   r
   r   Ú_make_entry_resource[   s,    #þ
zLogger._make_entry_resourcec                 C   s0   |   |¡}| j|||||d}|j |g¡ dS )aÈ  API call:  log a text message via a POST request

        See:
        https://cloud.google.com/logging/docs/api/ref_v2beta1/rest/v2beta1/entries/write

        :type text: text
        :param text: the log message.

        :type client: :class:`gcloud.logging.client.Client` or ``NoneType``
        :param client: the client to use.  If not passed, falls back to the
                       ``client`` stored on the current logger.

        :type labels: dict or :class:`NoneType`
        :param labels: (optional) mapping of labels for the entry.

        :type insert_id: string or :class:`NoneType`
        :param insert_id: (optional) unique ID for log entry.

        :type severity: string or :class:`NoneType`
        :param severity: (optional) severity of event being logged.

        :type http_request: dict or :class:`NoneType`
        :param http_request: (optional) info about HTTP request associated with
                             the entry
        )r!   r   r$   r   r%   N©r   r(   Úlogging_apiÚwrite_entries)r   r!   r	   r   r$   r   r%   Úentry_resourcer
   r
   r   Úlog_textž   s    
þzLogger.log_textc                 C   s0   |   |¡}| j|||||d}|j |g¡ dS )a×  API call:  log a structured message via a POST request

        See:
        https://cloud.google.com/logging/docs/api/ref_v2beta1/rest/v2beta1/entries/write

        :type info: dict
        :param info: the log entry information

        :type client: :class:`gcloud.logging.client.Client` or ``NoneType``
        :param client: the client to use.  If not passed, falls back to the
                       ``client`` stored on the current logger.

        :type labels: dict or :class:`NoneType`
        :param labels: (optional) mapping of labels for the entry.

        :type insert_id: string or :class:`NoneType`
        :param insert_id: (optional) unique ID for log entry.

        :type severity: string or :class:`NoneType`
        :param severity: (optional) severity of event being logged.

        :type http_request: dict or :class:`NoneType`
        :param http_request: (optional) info about HTTP request associated with
                             the entry
        )r"   r   r$   r   r%   Nr)   )r   r"   r	   r   r$   r   r%   r,   r
   r
   r   Ú
log_struct¿   s    
þzLogger.log_structc                 C   s0   |   |¡}| j|||||d}|j |g¡ dS )aæ  API call:  log a protobuf message via a POST request

        See:
        https://cloud.google.com/logging/docs/api/ref_v2beta1/rest/v2beta1/entries/write

        :type message: Protobuf message
        :param message: the message to be logged

        :type client: :class:`gcloud.logging.client.Client` or ``NoneType``
        :param client: the client to use.  If not passed, falls back to the
                       ``client`` stored on the current logger.

        :type labels: dict or :class:`NoneType`
        :param labels: (optional) mapping of labels for the entry.

        :type insert_id: string or :class:`NoneType`
        :param insert_id: (optional) unique ID for log entry.

        :type severity: string or :class:`NoneType`
        :param severity: (optional) severity of event being logged.

        :type http_request: dict or :class:`NoneType`
        :param http_request: (optional) info about HTTP request associated with
                             the entry
        )r#   r   r$   r   r%   Nr)   )r   r#   r	   r   r$   r   r%   r,   r
   r
   r   Ú	log_protoà   s    
þzLogger.log_protoc                 C   s    |   |¡}|j | j| j¡ dS )aŽ  API call:  delete all entries in a logger via a DELETE request

        See:
        https://cloud.google.com/logging/docs/api/ref_v2beta1/rest/v2beta1/projects.logs/delete

        :type client: :class:`gcloud.logging.client.Client` or ``NoneType``
        :param client: the client to use.  If not passed, falls back to the
                       ``client`` stored on the current logger.
        N)r   r*   Zlogger_deleter   r   r   r
   r
   r   Údelete  s    

zLogger.deletec                 C   s<   d| j f }|dur"d||f }n|}| jj|||||dS )a<  Return a page of log entries.

        See:
        https://cloud.google.com/logging/docs/api/ref_v2beta1/rest/v2beta1/entries/list

        :type projects: list of strings
        :param projects: project IDs to include. If not passed,
                            defaults to the project bound to the client.

        :type filter_: string
        :param filter_: a filter expression. See:
                        https://cloud.google.com/logging/docs/view/advanced_filters

        :type order_by: string
        :param order_by: One of :data:`gcloud.logging.ASCENDING` or
                         :data:`gcloud.logging.DESCENDING`.

        :type page_size: int
        :param page_size: maximum number of entries to return, If not passed,
                          defaults to a value set by the API.

        :type page_token: string
        :param page_token: opaque marker for the next "page" of entries. If not
                           passed, the API will return the first page of
                           entries.

        :rtype: tuple, (list, str)
        :returns: list of :class:`gcloud.logging.entry.TextEntry`, plus a
                  "next page token" string:  if not None, indicates that
                  more entries can be retrieved with another call (pass that
                  value as ``page_token``).
        z
logName=%sNz	%s AND %s)ÚprojectsÚfilter_Úorder_byÚ	page_sizeÚ
page_token)r   r	   Úlist_entries)r   r1   r2   r3   r4   r5   Z
log_filterr
   r
   r   r6     s    "þzLogger.list_entries)N)N)NNNNNNN)NNNNN)NNNNN)NNNNN)N)NNNNN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úpropertyr	   r   r   r   r   r   r(   r-   r.   r/   r0   r6   r
   r
   r
   r   r      s:   





   þ
C  ÿ
!  ÿ
!  ÿ
!
  ÿr   c                   @   sP   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	d
„Zddd„Zddd„Z	ddd„Z
dS )r   aH  Context manager:  collect entries to log via a single API call.

    Helper returned by :meth:`Logger.batch`

    :type logger: :class:`gcloud.logging.logger.Logger`
    :param logger: the logger to which entries will be logged.

    :type client: :class:`gcloud.logging.client.Client`
    :param client: The client to use.
    c                 C   s   || _ g | _|| _d S r   )ÚloggerÚentriesr	   )r   r<   r	   r
   r
   r   r   E  s    zBatch.__init__c                 C   s   | S r   r
   r   r
   r
   r   Ú	__enter__J  s    zBatch.__enter__c                 C   s   |d u r|   ¡  d S r   )Úcommit)r   Úexc_typeÚexc_valÚexc_tbr
   r
   r   Ú__exit__M  s    zBatch.__exit__Nc                 C   s   | j  d|||||f¡ dS )a  Add a text entry to be logged during :meth:`commit`.

        :type text: string
        :param text: the text entry

        :type labels: dict or :class:`NoneType`
        :param labels: (optional) mapping of labels for the entry.

        :type insert_id: string or :class:`NoneType`
        :param insert_id: (optional) unique ID for log entry.

        :type severity: string or :class:`NoneType`
        :param severity: (optional) severity of event being logged.

        :type http_request: dict or :class:`NoneType`
        :param http_request: (optional) info about HTTP request associated with
                             the entry.
        r!   N©r=   Úappend)r   r!   r   r$   r   r%   r
   r
   r   r-   Q  s    ÿzBatch.log_textc                 C   s   | j  d|||||f¡ dS )a  Add a struct entry to be logged during :meth:`commit`.

        :type info: dict
        :param info: the struct entry

        :type labels: dict or :class:`NoneType`
        :param labels: (optional) mapping of labels for the entry.

        :type insert_id: string or :class:`NoneType`
        :param insert_id: (optional) unique ID for log entry.

        :type severity: string or :class:`NoneType`
        :param severity: (optional) severity of event being logged.

        :type http_request: dict or :class:`NoneType`
        :param http_request: (optional) info about HTTP request associated with
                             the entry.
        ÚstructNrD   )r   r"   r   r$   r   r%   r
   r
   r   r.   h  s    ÿzBatch.log_structc                 C   s   | j  d|||||f¡ dS )a¥  Add a protobuf entry to be logged during :meth:`commit`.

        :type message: protobuf message
        :param message: the protobuf entry

        :type labels: dict or :class:`NoneType`
        :param labels: (optional) mapping of labels for the entry.

        :type insert_id: string or :class:`NoneType`
        :param insert_id: (optional) unique ID for log entry.

        :type severity: string or :class:`NoneType`
        :param severity: (optional) severity of event being logged.

        :type http_request: dict or :class:`NoneType`
        :param http_request: (optional) info about HTTP request associated with
                             the entry.
        ÚprotoNrD   )r   r#   r   r$   r   r%   r
   r
   r   r/     s    ÿzBatch.log_protoc                 C   s  |du r| j }| jjddidœ}| jjdur8| jj|d< g }| jD ]°\}}}}}}	|dkrdd|i}
nD|dkrvd	|i}
n2|d
kršt|ƒ}t |¡}d|i}
ntd|f ƒ‚|dur¸||
d< |durÈ||
d< |durØ||
d< |	durè|	|
d< | 	|
¡ qB|j
j|fi |¤Ž | jdd…= dS )a  Send saved log entries as a single API call.

        :type client: :class:`gcloud.logging.client.Client` or ``NoneType``
        :param client: the client to use.  If not passed, falls back to the
                       ``client`` stored on the current batch.
        Nr   r   )Zlogger_namer   r   r!   r   rF   r   rG   r   zUnknown entry type: %sr   r   r   )r	   r<   r   r   r=   r   r   r    Ú
ValueErrorrE   r*   r+   )r   r	   Úkwargsr=   Z
entry_typeÚentryr   Ziidr   Zhttp_reqr"   r&   r'   r
   r
   r   r?   –  s:    þ



zBatch.commit)NNNN)NNNN)NNNN)N)r7   r8   r9   r:   r   r>   rC   r-   r.   r/   r?   r
   r
   r
   r   r   :  s   
  ÿ
  ÿ
  ÿ
r   )r:   r   Zgoogle.protobuf.json_formatr   Úobjectr   r   r
   r
   r
   r   Ú<module>   s     &