Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basics of cloud logging. #1198

Merged
merged 6 commits into from
Feb 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@ focuser =
matplotlib
scipy
google =
google-cloud-storage
google-cloud-firestore
google-cloud-logging
google-cloud-storage
gsutil
protobuf==4.23.0
rsa
Expand Down
7 changes: 6 additions & 1 deletion src/panoptes/pocs/utils/cli/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from typing import List

import typer
from panoptes.utils.config.client import get_config
from panoptes.utils.time import current_time
from panoptes.utils.utils import altaz_to_radec
from rich import print
Expand All @@ -21,9 +22,13 @@

@app.callback()
def common(context: typer.Context,
simulator: List[str] = typer.Option(None, '--simulator', '-s', help='Simulators to load')
simulator: List[str] = typer.Option(None, '--simulator', '-s', help='Simulators to load'),
cloud_logging: bool = typer.Option(False, '--cloud-logging', '-c', help='Enable cloud logging'),
):
context.obj = simulator
if cloud_logging:
os.environ['CLOUD_LOGGING'] = True
os.environ['PANID'] = get_config('PANID')


def get_pocs(context: typer.Context):
Expand Down
15 changes: 15 additions & 0 deletions src/panoptes/pocs/utils/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@

from loguru import logger as loguru_logger

cloud_client = None
if bool(os.getenv('CLOUD_LOGGING', False)) is True:
with suppress(ImportError, NameError):
import google.cloud.logging
from google.cloud.logging_v2.handlers import CloudLoggingHandler

cloud_client = google.cloud.logging.Client()
cloud_client.setup_logging()


class PanLogger:
"""Custom formatter to have dynamic widths for logging.
Expand Down Expand Up @@ -122,6 +131,12 @@ def get_logger(console_log_file='panoptes.log',
level='TRACE')
LOGGER_INFO.handlers['archive'] = archive_id

if cloud_client is not None and 'cloud' not in LOGGER_INFO.handlers:
unit_id = os.getenv('PANID', os.environ['USER'])
cloud_handler = CloudLoggingHandler(cloud_client, name='panoptes-units', labels={'unit_id': unit_id})
cloud_id = loguru_logger.add(cloud_handler, level='DEBUG', format="{name} {function}:{line} {message}")
LOGGER_INFO.handlers['cloud'] = cloud_id

# Customize colors.
loguru_logger.level('TRACE', color='<cyan>')
loguru_logger.level('DEBUG', color='<white>')
Expand Down
Loading