
    	]jC                     j   d Z ddl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
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mZ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#  ejH                  e	d      Z%dZ&	 e%sddl'm(Z( dZ&e&xr e% Z*dZ+dZ,dZ-dZ. G d de      Z/y# e)$ r dZ&dZ(Y &w xY w)z9Client for interacting with the Google Cloud Logging API.    N)ClientWithProject)DISABLE_GRPC)_add_defaults_to_filter)
Connection)_LoggingAPI)_MetricsAPI)	_SinksAPI)CloudLoggingHandlerStructuredLogHandlersetup_logging)detect_resource)EXCLUDED_LOGGER_DEFAULTSLoggerMetric)ResourceSinkF)_gapicTgae_appk8s_containercloud_functioncloud_run_revisionc                       e Zd ZdZdZdZdZdZ	 ddddddd fd
Ze	d        Z
e	d        Ze	d        Zddd	d
ZddddddddZddddZddddddZddddZdddddZd Zej*                  eddZd Z fdZ xZS )Clientz7Client to bundle configuration needed for API requests.N)z,https://www.googleapis.com/auth/logging.readz-https://www.googleapis.com/auth/logging.writez-https://www.googleapis.com/auth/logging.adminz.https://www.googleapis.com/auth/cloud-platform)projectcredentials_http	_use_grpcclient_infoclient_optionsc                   t         t        |   ||||       d|i}|rVt        |t              r)t
        j                  j                  j                  |      }|j                  r|j                  }||d<   t        | fi || _        || j                  j                  }|| _        || _        |t        | _        n|| _        t!               | _        y)a  
        Args:
            project (Optional[str]): the project which the client acts on behalf of.
                If not passed, falls back to the default inferred
                from the environment.
            credentials (Optional[google.auth.credentials.Credentials]):
                Thehe OAuth2 Credentials to use for this
                client. If not passed (and if no ``_http`` object is
                passed), falls back to the default inferred from the
                environment.
            _http (Optional[requests.Session]):  HTTP object to make requests.
                Can be any object that defines ``request()`` with the same interface as
                :meth:`requests.Session.request`. If not passed, an
                ``_http`` object is created that is bound to the
                ``credentials`` for the current object.
                This parameter should be considered private, and could
                change in the future.
            _use_grpc (Optional[bool]): Explicitly specifies whether
                to use the gRPC transport or HTTP. If unset,
                falls back to the ``GOOGLE_CLOUD_DISABLE_GRPC``
                environment variable
                This parameter should be considered private, and could
                change in the future.
            client_info (Optional[Union[google.api_core.client_info.ClientInfo, google.api_core.gapic_v1.client_info.ClientInfo]]):
                The client info used to send a user-agent string along with API
                requests. If ``None``, then default info will be used. Generally,
                you only need to set this if you're developing your own library
                or partner tool.
            client_options (Optional[Union[dict, google.api_core.client_options.ClientOptions]]):
                Client options used to set user options
                on the client. API Endpoint should be set through client_options.
        )r   r   r   r"   r!   api_endpointN)superr   __init__
isinstancedictgoogleapi_corer"   	from_dictr$   r   _connection_client_info_client_options	_USE_GRPCr    set	_handlers)
selfr   r   r   r    r!   r"   kw_argsr$   	__class__s
            H/root/env/lib/python3.12/site-packages/google/cloud/logging_v2/client.pyr&   zClient.__init__O   s    T 	fd$#)	 	% 	
 !+..$/!'!?!?!I!I"" **-::*6'%d6g6 **77K'-&DN&DN    c                     | j                   B| j                  r&t        j                  |       | _         | j                   S t	        |       | _         | j                   S )zHelper for logging-related API calls.

        See
        https://cloud.google.com/logging/docs/reference/v2/rest/v2/entries
        https://cloud.google.com/logging/docs/reference/v2/rest/v2/projects.logs
        )_logging_apir    r   make_logging_apiJSONLoggingAPIr2   s    r5   logging_apizClient.logging_api   sS     $~~$*$;$;D$A!     %34$8!   r6   c                     | j                   B| j                  r&t        j                  |       | _         | j                   S t	        |       | _         | j                   S )zHelper for log sink-related API calls.

        See
        https://cloud.google.com/logging/docs/reference/v2/rest/v2/projects.sinks
        )
_sinks_apir    r   make_sinks_apiJSONSinksAPIr;   s    r5   	sinks_apizClient.sinks_api   sK     ??"~~"("7"7"=  #/t"4r6   c                     | j                   B| j                  r&t        j                  |       | _         | j                   S t	        |       | _         | j                   S )zHelper for log metric-related API calls.

        See
        https://cloud.google.com/logging/docs/reference/v2/rest/v2/projects.metrics
        )_metrics_apir    r   make_metrics_apiJSONMetricsAPIr;   s    r5   metrics_apizClient.metrics_api   sS     $~~$*$;$;D$A!     %34$8!   r6   )labelsresourcec                     t        || ||      S )a3  Creates a logger bound to the current client.

        Args:
            name (str): The name of the logger to be constructed.
            resource (Optional[~logging_v2.Resource]): a monitored resource object
                representing the resource the code was run on. If not given, will
                be inferred from the environment.
            labels (Optional[dict]): Mapping of default labels for entries written
                via this logger.

        Returns:
            ~logging_v2.logger.Logger: Logger created with the current client.
        )clientrG   rH   r   )r2   namerG   rH   s       r5   loggerzClient.logger   s     d4JJr6   resource_namesfilter_order_bymax_results	page_size
page_tokenc                ~    |d| j                    g}t        |      }| j                  j                  ||||||      S )a|  Return a generator of log entry resources.

        Args:
            resource_names (Sequence[str]): Names of one or more parent resources
                from which to retrieve log entries:

                ::

                    "projects/[PROJECT_ID]"
                    "organizations/[ORGANIZATION_ID]"
                    "billingAccounts/[BILLING_ACCOUNT_ID]"
                    "folders/[FOLDER_ID]"

                If not passed, defaults to the project bound to the API's client.

            filter_ (str): a filter expression. See
                https://cloud.google.com/logging/docs/view/advanced_filters
            order_by (str) One of :data:`~logging_v2.ASCENDING`
                or :data:`~logging_v2.DESCENDING`.
            max_results (Optional[int]):
                Optional. The maximum number of entries to return.
                Non-positive values are treated as 0. If None, uses API defaults.
            page_size (int): number of entries to fetch in each API call. Although
                requests are paged internally, logs are returned by the generator
                one at a time. If not passed, defaults to a value set by the API.
            page_token (str): opaque marker for the starting "page" of entries. If not
                passed, the API will return the first page of entries.

        Returns:
            Generator[~logging_v2.LogEntry]
        	projects/rM   )r   r   r<   list_entries)r2   rN   rO   rP   rQ   rR   rS   s          r5   rV   zClient.list_entries   sX    R ! )$,,89N)'2,,)#! - 
 	
r6   )rO   destinationc                     t        ||||       S )a  Creates a sink bound to the current client.

        Args:
            name (str): the name of the sink to be constructed.
            filter_ (Optional[str]): the advanced logs filter expression
                defining the entries exported by the sink.  If not
                passed, the instance should already exist, to be
                refreshed via :meth:`Sink.reload`.
            destination (str): destination URI for the entries exported by
                the sink.  If not passed, the instance should
                already exist, to be refreshed via
                :meth:`Sink.reload`.

        Returns:
            ~logging_v2.sink.Sink: Sink created with the current client.
        )rO   rW   rJ   r   )r2   rK   rO   rW   s       r5   sinkzClient.sink
  s    " D'{4PPr6   parentrQ   rR   rS   c                b    |d| j                    }| j                  j                  ||||      S )a  List sinks for the a parent resource.

        See
        https://cloud.google.com/logging/docs/reference/v2/rest/v2/projects.sinks/list

        Args:
            parent (Optional[str]): The parent resource whose sinks are to be listed:

                ::

                    "projects/[PROJECT_ID]"
                    "organizations/[ORGANIZATION_ID]"
                    "billingAccounts/[BILLING_ACCOUNT_ID]"
                    "folders/[FOLDER_ID]".

                If not passed, defaults to the project bound to the API's client.
            max_results (Optional[int]):
                Optional. The maximum number of entries to return.
                Non-positive values are treated as 0. If None, uses API defaults.
            page_size (int): number of entries to fetch in each API call. Although
                requests are paged internally, logs are returned by the generator
                one at a time. If not passed, defaults to a value set by the API.
            page_token (str): opaque marker for the starting "page" of entries. If not
                passed, the API will return the first page of entries.

        Returns:
            Generator[~logging_v2.Sink]
        rU   rZ   )r   rA   
list_sinks)r2   r[   rQ   rR   rS   s        r5   r]   zClient.list_sinks  sA    > > /F~~((#!	 ) 
 	
r6    )rO   descriptionc                     t        ||| |      S )a  Creates a metric bound to the current client.

        Args:
            name (str): The name of the metric to be constructed.
            filter_(Optional[str]): The advanced logs filter expression defining the
                entries tracked by the metric.  If not
                passed, the instance should already exist, to be
                refreshed via :meth:`Metric.reload`.
            description (Optional[str]): The description of the metric to be constructed.
                If not passed, the instance should already exist,
                to be refreshed via :meth:`Metric.reload`.

        Returns:
            ~logging_v2.metric.Metric: Metric created with the current client.
        )rO   rJ   r_   r   )r2   rK   rO   r_   s       r5   metriczClient.metricE  s      dGDkRRr6   rQ   rR   rS   c                T    | j                   j                  | j                  |||      S )aE  List metrics for the project associated with this client.

        See
        https://cloud.google.com/logging/docs/reference/v2/rest/v2/projects.metrics/list

        Args:
            max_results (Optional[int]):
                Optional. The maximum number of entries to return.
                Non-positive values are treated as 0. If None, uses API defaults.
            page_size (int): number of entries to fetch in each API call. Although
                requests are paged internally, logs are returned by the generator
                one at a time. If not passed, defaults to a value set by the API.
            page_token (str): opaque marker for the starting "page" of entries. If not
                passed, the API will return the first page of entries.

        Returns:
            Generator[logging_v2.Metric]
        rb   )rF   list_metricsr   )r2   rQ   rR   rS   s       r5   rd   zClient.list_metricsW  s2    & ,,LL#!	 - 
 	
r6   c                    |j                  dt        | j                              }t        |t              r|j
                  t        k(  rt        | fd|i|S |j
                  t        k(  rt        di |d| j                  iS |j
                  t        k(  r<|j                  dt        j                        |d<   t        di |d| j                  iS |j
                  t        k(  rt        di |d| j                  iS t        | fd|i|S )zReturn the default logging handler based on the local environment.

        Args:
            kw (dict): keyword args passed to handler constructor

        Returns:
            logging.Handler: The default log handler based on the environment
        rH   
project_idstream )popr   r   r'   r   type_GAE_RESOURCE_TYPEr
   _GKE_RESOURCE_TYPEr   _GCF_RESOURCE_TYPEgetsys
__stdout___RUN_RESOURCE_TYPE)r2   kwmonitored_resources      r5   get_default_handlerzClient.get_default_handlerq  s      VVJ0MN((3!&&*<<*4S:LSPRSS#((,>>+JbJT\\JJ#((,>>!vvh?8+JbJT\\JJ#((,>>+JbJT\\JJ"4K2DKKKr6   	log_levelexcluded_loggersc                z     | j                   di |}| j                  j                  |       t        |||       y)aU  Attach default Cloud Logging handler to the root logger.

        This method uses the default log handler, obtained by
        :meth:`~get_default_handler`, and attaches it to the root Python
        logger, so that a call such as ``logging.warn``, as well as all child
        loggers, will report to Cloud Logging.

        Args:
            log_level (Optional[int]): The logging level threshold of the attached logger,
                as set by the :meth:`logging.Logger.setLevel` method. Defaults to
                :const:`logging.INFO`.
            excluded_loggers (Optional[Tuple[str]]): The loggers to not attach the
                handler to. This will always include the
                loggers in the path of the logging client
                itself.
        Returns:
            dict: keyword args passed to handler constructor
        ru   Nrh   )rt   r1   addr   )r2   rv   rw   rr   handlers        r5   r   zClient.setup_logging  s8    * +$**0R07#gEUVr6   c                 F    | j                   D ]  }|j                           y)z<Flushes all Python log handlers associated with this Client.N)r1   flush)r2   rz   s     r5   flush_handlerszClient.flush_handlers  s      ~~ 	GMMO	r6   c                 l    t         t        |           | j                  D ]  }|j                           y)z?Closes the Client and all handlers associated with this Client.N)r%   r   closer1   )r2   rz   r4   s     r5   r   zClient.close  s,    fd!#~~ 	GMMO	r6   )__name__
__module____qualname____doc__r8   r>   rC   SCOPEr&   propertyr<   rA   rF   rL   rV   rY   r]   ra   rd   rt   loggingINFOr   r   r}   r   __classcell__)r4   s   @r5   r   r   @   s    ALJLE H
 HT ! !   ! ! &*D K& 4
l %)d Q( $$4&
P '+ S$ +/$4 
4L2 #<<:RW2 r6   r   )0r   r   osro   google.api_core.client_optionsr)   google.cloud.clientr   google.cloud.environment_varsr    google.cloud.logging_v2._helpersr   google.cloud.logging_v2._httpr   r   r:   r   rE   r	   r@    google.cloud.logging_v2.handlersr
   r   r   5google.cloud.logging_v2.handlers._monitored_resourcesr   )google.cloud.logging_v2.handlers.handlersr   google.cloud.logging_v2.loggerr   google.cloud.logging_v2.metricr    google.cloud.logging_v2.resourcer   google.cloud.logging_v2.sinkr   getenv_DISABLE_GRPC
_HAVE_GRPCgoogle.cloud.logging_v2r   ImportErrorr/   rk   rl   rm   rq   r   rh   r6   r5   <module>r      s    @  	 
 % 1 6 D 4 G G C 
 R N 1 1 5 -		,.
	2
 ,},	 $ % ) l l  JFs   
B& &	B21B2