Skip to content
Draft
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
12 changes: 7 additions & 5 deletions client/ayon_core/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import sys
import logging
import code
import traceback

Check failure on line 7 in client/ayon_core/cli.py

View workflow job for this annotation

GitHub Actions / linting

ruff (F401)

client/ayon_core/cli.py:7:8: F401 `traceback` imported but unused help: Remove unused import: `traceback`
from pathlib import Path

import click
Expand All @@ -24,7 +24,9 @@
)


log = Logger.get_logger("CLI")

@click.group(invoke_without_command=True)

Check failure on line 29 in client/ayon_core/cli.py

View workflow job for this annotation

GitHub Actions / linting

ruff (E302)

client/ayon_core/cli.py:29:1: E302 Expected 2 blank lines, found 1 help: Add missing blank line(s)
@click.pass_context
@click.option(
"--use-staging",
Expand Down Expand Up @@ -332,10 +334,10 @@
sys.path.insert(0, path)
os.environ["PYTHONPATH"] = os.pathsep.join(split_paths)

print(">>> loading environments ...")
print(" - global AYON ...")
log.info("loading environments ...")
log.debug("[yellow]- global AYON ...[/yellow]")
_set_global_environments()
print(" - for addons ...")
log.debug("[yellow]- for addons ...[/yellow]")
addons_manager = AddonsManager()
_set_addons_environments(addons_manager)
_add_addons(addons_manager)
Expand All @@ -350,6 +352,6 @@
)
except Exception: # noqa
exc_info = sys.exc_info()
print("!!! AYON crashed:")
traceback.print_exception(*exc_info)
log.critical("AYON crashed:", exc_info=exc_info)
# traceback.print_exception(*exc_info)
sys.exit(1)
94 changes: 93 additions & 1 deletion client/ayon_core/lib/log.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import contextlib
import os
import sys
import getpass
Expand All @@ -7,11 +8,21 @@
import time
import threading
import copy
from loguru import logger
from dataclasses import dataclass

from . import Terminal


@dataclass
class ProcessData:
hostname: str
hostip: str
username: str
system_name: str
process_name: str
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also have a process session uuid - so that if you wanted to filter to all logs from a particular session, then this is the go to way.

Then additionally, we should perhaps pass process_session_id to the child processes so they can store parent_process_session_id so that from one session, one could move upstream to the parent sessions?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is something that was already there - I just copied it there to maintain compatibility. Something like this would have to be part of the logs definitely, but added to the sink/handler differently.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see - it's taken from here.


class LogStreamHandler(logging.StreamHandler):

Check failure on line 25 in client/ayon_core/lib/log.py

View workflow job for this annotation

GitHub Actions / linting

ruff (E302)

client/ayon_core/lib/log.py:25:1: E302 Expected 2 blank lines, found 1 help: Add missing blank line(s)
"""StreamHandler class.

This was originally designed to handle UTF errors in python 2.x hosts,
Expand Down Expand Up @@ -91,7 +102,88 @@
return out


class Logger:
class Logger():
"""Logger class.

This is a wrapper for loguru logger. It is used to provide a unified
interface for logging in AYON. It also provides a way to disable logging
in some cases, for example when running in a headless environment.

"""

# Data same for all record documents
process_data: ProcessData = None

@classmethod
def get_process_data(cls):
"""Data about current process which should be same for all records.

Process data are used for each record sent to mongo database.
"""
if cls.process_data is not None:
return copy.deepcopy(cls.process_data)

if not cls.initialized:
cls.initialize()

host_name = socket.gethostname()
try:
host_ip = socket.gethostbyname(host_name)
except socket.gaierror:
host_ip = "127.0.0.1"

process_name = cls.get_process_name()

cls.process_data = ProcessData(
hostname=host_name,
hostip=host_ip,
username=getpass.getuser(),
system_name=platform.system(),
process_name=process_name
)

return copy.deepcopy(cls.process_data)

@classmethod
def get_logger(cls, name=None):
return logger.bind(name=name or "__main__")

@classmethod
def get_process_name(cls) -> str:
"""Process name that is like "label" of a process.

AYON logging can be used from OpenPyppe itself of from hosts.
Even in AYON process it's good to know if logs are from tray or
from other cli commands. This should help to identify that information.
"""
if cls._process_name is not None:
return cls._process_name

# Get process name
process_name = os.environ.get("AYON_APP_NAME")
if not process_name:
with contextlib.suppress(ImportError):
import psutil
process = psutil.Process(os.getpid())
process_name = process.name()

if not process_name:
process_name = os.path.basename(sys.executable)

cls._process_name = process_name
return cls._process_name

@classmethod
def set_process_name(cls, process_name: str) -> None:
"""Set process name for mongo logs."""
# Just change the attribute
cls._process_name = process_name
# Update process data if are already set
if cls.process_data is not None:
cls.process_data.process_name = process_name


class Legacy_Logger:
DFT = '%(levelname)s >>> { %(name)s }: [ %(message)s ] '
DBG = " - { %(name)s }: [ %(message)s ] "
INF = ">>> [ %(message)s ] "
Expand Down
5 changes: 3 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ dependencies = [
"pytest-print >=1.0.0",
"ayon-python-api >=1.0.0",
"arrow ==0.17.0",
"ruff >=0.11.7",
"ruff >=0.15.4",
"pre-commit >=4.0.0",
"codespell >=2.2.6",
"semver >=3.0.2",
Expand All @@ -34,7 +34,8 @@ dependencies = [
"speedcopy >=2.1.0",
"qtpy >=2.4.3",
"pyside6 >=6.5.2",
"pytest-ayon @ git+https://github.com/ynput/pytest-ayon.git"
"pytest-ayon @ git+https://github.com/ynput/pytest-ayon.git",
"loguru (>=0.7.3,<0.8.0)"
]

[tool.codespell]
Expand Down
Loading