Skip to main content

custom_logging.py

Source: src/sunholo/custom_logging.py

Functions

get_logger()

No docstring available.

is_logging_setup(logger=None)

No docstring available.

log_folder_location(folder_name)

No docstring available.

safe_log_struct(log, severity, message, struct)

No docstring available.

setup_logging(logger_name=None, log_level=20, project_id=None)

Sets up Google Cloud Logging with the provided log level and project ID. If no project ID is provided, it attempts to retrieve the project ID from the metadata server.

Parameters: logger_name (str): The name of the log to send to. If not provided, set to run.googleapis.com%2Fstderr log_level: The logging level to capture. Uses Python's logging module levels. Default is log.INFO. project_id: A string representing the Google Cloud project ID. If None, the project ID will be retrieved from the metadata server.

Example:

Set up Google Cloud Logging for the script with default INFO level

setup_logging()

Now you can use Python's logging module as usual

import logging log.info('This is an info message that will be sent to Google Cloud log.')

Basic structured logging

log.info(log_struct={"action": "user_login", "user_id": "12345"})

Structured logging with trace ID

log.update_trace_id("abc-123") log.info(log_struct={"action": "process_started", "file_count": 42})

This will include trace_id: "abc-123" in the logged structure

Logging with both text and structure

log.info( log_text="Processing completed successfully", log_struct={"duration_ms": 1234, "items_processed": 100} )

Logging error with structured context

try:

Some operation

process_data() except Exception as e: log.error( log_text=f"Error processing data: {str(e)}", log_struct={ "error_type": type(e).name, "file_name": "example.csv", "line_number": 42 } )

More complex structured logging

log.info(log_struct={ "request": { "method": "POST", "path": "/api/data", "user_agent": "Mozilla/5.0...", "ip": "192.168.1.1" }, "response": { "status_code": 200, "processing_time_ms": 345, "bytes_sent": 1024 }, "metadata": { "version": "1.2.3", "environment": "production" } })

Note: This function requires that the 'google-cloud-logging' library is installed and that the application is authenticated with Google Cloud. This can be done by setting the GOOGLE_APPLICATION_CREDENTIALS environment variable to the path of your service account key file, or by running this code in an environment where default application credentials are already set, such as Google Cloud Compute Engine, Google Kubernetes Engine, Google App Engine, etc.

Classes

GoogleCloudLogging

No docstring available.

  • init(self, project_id=None, log_level=20, logger_name=None)

    • Initialize self. See help(type(self)) for accurate signature.
  • new(cls, project_id=None, log_level=20, logger_name=None)

    • Create and return a new object. See help(type) for accurate signature.
  • _add_trace_to_struct(self, log_struct)

    • Adds trace ID to structured log data if available.

Args: log_struct (dict): The structured log data.

Returns: dict: Log structure with trace ID added if available.

  • _append_trace_id(self, log_text)
    • Appends trace ID to log text if available.

Args: log_text (str): The log message.

Returns: str: Log message with trace ID prefixed if available.

  • _get_caller_info(self)

    • Internal method to get caller's filename, line number, and function name.
  • debug(self, log_text=None, log_struct=None)

    • Writes a debug log entry.

Args: log_text (str, optional): The debug log message as a text string. Defaults to None. log_struct (dict, optional): The debug log message as structured data. Defaults to None. logger_name (str, optional): The name of the logger to which to write the debug log entry.

  • error(self, log_text=None, log_struct=None)
    • Writes an error log entry.

Args: log_text (str, optional): The error log message as a text string. Defaults to None. log_struct (dict, optional): The error log message as structured data. Defaults to None. logger_name (str, optional): The name of the logger to which to write the error log entry.

  • exception(self, log_text=None, log_struct=None)
    • Writes an exception log entry.

Args: log_text (str, optional): The error log message as a text string. Defaults to None. log_struct (dict, optional): The error log message as structured data. Defaults to None. logger_name (str, optional): The name of the logger to which to write the error log entry.

  • info(self, log_text=None, log_struct=None)
    • Writes an info log entry.

Args: log_text (str, optional): The info log message as a text string. Defaults to None. log_struct (dict, optional): The info log message as structured data. Defaults to None. logger_name (str, optional): The name of the logger to which to write the info log entry.

  • log(self, message, *args, **kwargs)
    • Some weird bug keeps calling this method - do not use normally

A catch-all method to handle unexpected .log() calls on this class. Routes the call to the appropriate logging method based on severity level.

  • setup_logging(self, log_level=20, logger_name=None)

    • No docstring available.
  • structured_log(self, log_text=None, log_struct=None, logger_name=None, severity='INFO')

    • Writes log entries to the specified logger as either text or structured data.

Args: log_text (str, optional): The log message as a text string. Defaults to None. log_struct (dict, optional): The log message as a dictionary for structured log. Defaults to None. logger_name (str, optional): The name of the logger to which to write the log entries. e.g. logName="run.googleapis.com%2Fstderr" severity (str, optional): The severity level of the log entry. Defaults to "INFO".

  • update_trace_id(self, trace_id)
    • Updates the trace ID to be included in all logs.

Args: trace_id (str): The trace ID to add to all logs.

  • warning(self, log_text=None, log_struct=None)
    • Writes a warning log entry.

Args: log_text (str, optional): The warning log message as a text string. Defaults to None. log_struct (dict, optional): The warning log message as structured data. Defaults to None. logger_name (str, optional): The name of the logger to which to write the warning log entry.

StandardLoggerWrapper

A wrapper for standard Python logger that mimics the interface of GoogleCloudLogging.

  • getattr(self, name)

    • No docstring available.
  • init(self, logger)

    • Initialize self. See help(type(self)) for accurate signature.
  • _format_message(self, log_text=None, log_struct=None, severity=None)

    • Format message to include structured data as JSON
  • debug(self, log_text=None, log_struct=None)

    • No docstring available.
  • error(self, log_text=None, log_struct=None)

    • No docstring available.
  • exception(self, log_text=None, log_struct=None)

    • No docstring available.
  • info(self, log_text=None, log_struct=None)

    • No docstring available.
  • set_version(self, version)

    • Set version to be included in all logs.
  • structured_log(self, log_text=None, log_struct=None, logger_name=None, severity='INFO')

    • Emulates Google Cloud's structured_log method using standard logging.
  • update_trace_id(self, trace_id)

    • Set trace ID to be included in all logs.
  • warning(self, log_text=None, log_struct=None)

    • No docstring available.
Sunholo Multivac

Get in touch to see if we can help with your GenAI project.

Contact us

Other Links

Sunholo Multivac - GenAIOps

Copyright ©

Holosun ApS 2025