nyssa-ai-logging 0.2.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. nyssa_ai_logging-0.2.2/.gitignore +38 -0
  2. nyssa_ai_logging-0.2.2/PKG-INFO +81 -0
  3. nyssa_ai_logging-0.2.2/README.md +53 -0
  4. nyssa_ai_logging-0.2.2/pyproject.toml +66 -0
  5. nyssa_ai_logging-0.2.2/src/nyssa_ai_logging/__init__.py +48 -0
  6. nyssa_ai_logging-0.2.2/src/nyssa_ai_logging/configuration/__init__.py +4 -0
  7. nyssa_ai_logging-0.2.2/src/nyssa_ai_logging/configuration/composite_logger.py +93 -0
  8. nyssa_ai_logging-0.2.2/src/nyssa_ai_logging/configuration/create_nyssa_logging.py +23 -0
  9. nyssa_ai_logging-0.2.2/src/nyssa_ai_logging/configuration/logging_builder.py +124 -0
  10. nyssa_ai_logging-0.2.2/src/nyssa_ai_logging/console_logging/__init__.py +4 -0
  11. nyssa_ai_logging-0.2.2/src/nyssa_ai_logging/console_logging/console_output_logger.py +147 -0
  12. nyssa_ai_logging-0.2.2/src/nyssa_ai_logging/console_logging/console_output_logger_provider.py +29 -0
  13. nyssa_ai_logging-0.2.2/src/nyssa_ai_logging/console_logging/stderr_output_logger.py +147 -0
  14. nyssa_ai_logging-0.2.2/src/nyssa_ai_logging/console_logging/stderr_output_logger_provider.py +29 -0
  15. nyssa_ai_logging-0.2.2/src/nyssa_ai_logging/file_logging/__init__.py +4 -0
  16. nyssa_ai_logging-0.2.2/src/nyssa_ai_logging/file_logging/clock.py +18 -0
  17. nyssa_ai_logging-0.2.2/src/nyssa_ai_logging/file_logging/json_file_logger.py +152 -0
  18. nyssa_ai_logging-0.2.2/src/nyssa_ai_logging/file_logging/json_file_logger_provider.py +67 -0
  19. nyssa_ai_logging-0.2.2/src/nyssa_ai_logging/file_logging/log_file.py +208 -0
  20. nyssa_ai_logging-0.2.2/src/nyssa_ai_logging/file_logging/log_record.py +29 -0
  21. nyssa_ai_logging-0.2.2/src/nyssa_ai_logging/file_logging/log_record_serializer.py +60 -0
  22. nyssa_ai_logging-0.2.2/src/nyssa_ai_logging/file_logging/owner_only_access.py +91 -0
  23. nyssa_ai_logging-0.2.2/src/nyssa_ai_logging/file_logging/stderr_diagnostics.py +46 -0
  24. nyssa_ai_logging-0.2.2/src/nyssa_ai_logging/file_logging/system_clock.py +17 -0
  25. nyssa_ai_logging-0.2.2/src/nyssa_ai_logging/log_level.py +63 -0
  26. nyssa_ai_logging-0.2.2/src/nyssa_ai_logging/logger.py +82 -0
  27. nyssa_ai_logging-0.2.2/src/nyssa_ai_logging/logger_provider.py +24 -0
  28. nyssa_ai_logging-0.2.2/test/__init__.py +1 -0
  29. nyssa_ai_logging-0.2.2/test/unit/__init__.py +1 -0
  30. nyssa_ai_logging-0.2.2/test/unit/helpers/__init__.py +1 -0
  31. nyssa_ai_logging-0.2.2/test/unit/helpers/fixed_clock.py +25 -0
  32. nyssa_ai_logging-0.2.2/test/unit/helpers/stderr_capture.py +40 -0
  33. nyssa_ai_logging-0.2.2/test/unit/helpers/stdout_capture.py +40 -0
  34. nyssa_ai_logging-0.2.2/test/unit/helpers/temp_log_workspace.py +41 -0
  35. nyssa_ai_logging-0.2.2/test/unit/test_log_file.py +51 -0
  36. nyssa_ai_logging-0.2.2/test/unit/test_log_file_rotation.py +117 -0
  37. nyssa_ai_logging-0.2.2/test/unit/test_log_record_serializer.py +65 -0
  38. nyssa_ai_logging-0.2.2/test/unit/test_logging_installation.py +73 -0
  39. nyssa_ai_logging-0.2.2/test/unit/test_owner_only_access.py +34 -0
  40. nyssa_ai_logging-0.2.2/test/unit/test_stderr_diagnostics.py +27 -0
  41. nyssa_ai_logging-0.2.2/test/use_case/__init__.py +1 -0
  42. nyssa_ai_logging-0.2.2/test/use_case/test_logging_tutorials.py +166 -0
@@ -0,0 +1,38 @@
1
+ # Ignore dot directories at every level (tracked files remain tracked).
2
+ .*/
3
+ !.github/
4
+ !.github/**
5
+ !.nyssa-ai/
6
+ !.nyssa-ai/dev-standards/
7
+ !.nyssa-ai/dev-standards/**
8
+ !.compound-engineering/
9
+ !.compound-engineering/config.yaml
10
+ !.compound-engineering/config.example.yaml
11
+
12
+ **/bin/
13
+ **/obj/
14
+ **/TestResults/
15
+ *.user
16
+ *.suo
17
+ *.nupkg
18
+ *.snupkg
19
+ .env
20
+ .env.*
21
+ !.env.example
22
+ **/secrets.json
23
+ **/config-manifest.json
24
+ !docs/specs/**/config-manifest.json
25
+
26
+ # Build, dependency, and test runner outputs
27
+ __pycache__/
28
+ *.py[cod]
29
+ .pytest_cache/
30
+ .mypy_cache/
31
+ node_modules/
32
+ dist/
33
+
34
+ # Mandatory repository-local scratch directory (REPO-SCRATCH)
35
+ .temp/
36
+ .worktrees/
37
+ .compound-engineering/*.local.yaml
38
+ .context/compound-engineering/
@@ -0,0 +1,81 @@
1
+ Metadata-Version: 2.5
2
+ Name: nyssa-ai-logging
3
+ Version: 0.2.2
4
+ Summary: Lightweight operational logging destinations with rotating JSON files and clean protocol streams matching NyssaAI.Logging.
5
+ Project-URL: Homepage, https://github.com/NyssaAI/NyssaAI.Shared
6
+ Project-URL: Repository, https://github.com/NyssaAI/NyssaAI.Shared.git
7
+ Project-URL: Issues, https://github.com/NyssaAI/NyssaAI.Shared/issues
8
+ Author-email: NyssaAI <oss@nyssa.ai>
9
+ License: Apache-2.0
10
+ Keywords: jsonl,logging,rotating,stderr,structured-logging
11
+ Classifier: Development Status :: 4 - Beta
12
+ Classifier: Intended Audience :: Developers
13
+ Classifier: License :: OSI Approved :: Apache Software License
14
+ Classifier: Operating System :: OS Independent
15
+ Classifier: Programming Language :: Python :: 3
16
+ Classifier: Programming Language :: Python :: 3.12
17
+ Classifier: Programming Language :: Python :: 3.13
18
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
19
+ Classifier: Topic :: System :: Logging
20
+ Classifier: Typing :: Typed
21
+ Requires-Python: >=3.12
22
+ Provides-Extra: dev
23
+ Requires-Dist: mypy>=1.9.0; extra == 'dev'
24
+ Requires-Dist: pytest-cov>=4.0.0; extra == 'dev'
25
+ Requires-Dist: pytest>=8.0.0; extra == 'dev'
26
+ Requires-Dist: ruff>=0.3.0; extra == 'dev'
27
+ Description-Content-Type: text/markdown
28
+
29
+ # nyssa-ai-logging
30
+
31
+ `nyssa-ai-logging` provides lightweight, operational logging destinations for Python systems, matching the operational behavior and JSON schema of `NyssaAI.Logging` (`nyssa-ai-logging-cs`) and `@nyssa-ai/logging` (`nyssa-ai-logging-ts`). It delivers essential operational sinks—structured rotating JSON files, owner-only access controls, independent STDERR/STDOUT routing, and fail-safe execution—without introducing heavy external logging dependencies.
32
+
33
+ * **Rotating JSONL persistence**: Automatically rotates at 10 MiB with 5 numbered backups and 30-day retention.
34
+ * **Clean protocol streams**: Routes diagnostics to `sys.stderr` by default while keeping `sys.stdout` unpolluted for machine protocols (e.g. CLI pipes, MCP servers).
35
+ * **Cross-account privacy**: Enforces POSIX 0600 file modes / 0700 directories and Windows DACLs to prevent unauthorized cross-account log reads.
36
+ * **Crash-proof execution**: Degrades gracefully to STDERR diagnostics without throwing exceptions or aborting execution on sink errors.
37
+ * **Zero-ceremony integration**: Provides a fluent configuration builder and an action-taking `Logger` protocol for Dependency Injection.
38
+
39
+ ## Simple setup, ordinary logging
40
+
41
+ Register logging during startup, then inject `Logger` into the classes that need it. `create_nyssa_logging()` registers standard error output by default, with `Information` and higher levels enabled:
42
+
43
+ ```python
44
+ from nyssa_ai_logging import create_nyssa_logging
45
+
46
+ logging = (
47
+ create_nyssa_logging("logs/service.log")
48
+ .with_rotating_json_log("logs/application.json")
49
+ .with_console()
50
+ )
51
+
52
+ logger = logging.create_logger("OrderService")
53
+ logger.info("Order {OrderId} placed", {"OrderId": 1042}, operation="OrderPlaced")
54
+ ```
55
+
56
+ ## The JSON file
57
+
58
+ The log file contains one complete JSON object per line matching the cross-language record schema:
59
+
60
+ ```json
61
+ {
62
+ "Timestamp": "2026-10-12T14:23:45.123456+00:00",
63
+ "Level": "Information",
64
+ "Message": "Order 1042 placed",
65
+ "Module": "OrderService",
66
+ "Operation": "OrderPlaced",
67
+ "Exception": null,
68
+ "AdditionalFields": {
69
+ "OrderId": 1042
70
+ }
71
+ }
72
+ ```
73
+
74
+ ## Rotation and retention
75
+
76
+ | Setting | Behavior |
77
+ | --- | --- |
78
+ | Rotation threshold | 10 MiB, or 10,485,760 bytes. |
79
+ | Backup count | Up to five numbered backups beside the active file. |
80
+ | Backup names | `service.log.1` is the newest; `service.log.5` is the oldest. |
81
+ | Age limit | Backups older than 30 days are pruned at startup and during rotation. |
@@ -0,0 +1,53 @@
1
+ # nyssa-ai-logging
2
+
3
+ `nyssa-ai-logging` provides lightweight, operational logging destinations for Python systems, matching the operational behavior and JSON schema of `NyssaAI.Logging` (`nyssa-ai-logging-cs`) and `@nyssa-ai/logging` (`nyssa-ai-logging-ts`). It delivers essential operational sinks—structured rotating JSON files, owner-only access controls, independent STDERR/STDOUT routing, and fail-safe execution—without introducing heavy external logging dependencies.
4
+
5
+ * **Rotating JSONL persistence**: Automatically rotates at 10 MiB with 5 numbered backups and 30-day retention.
6
+ * **Clean protocol streams**: Routes diagnostics to `sys.stderr` by default while keeping `sys.stdout` unpolluted for machine protocols (e.g. CLI pipes, MCP servers).
7
+ * **Cross-account privacy**: Enforces POSIX 0600 file modes / 0700 directories and Windows DACLs to prevent unauthorized cross-account log reads.
8
+ * **Crash-proof execution**: Degrades gracefully to STDERR diagnostics without throwing exceptions or aborting execution on sink errors.
9
+ * **Zero-ceremony integration**: Provides a fluent configuration builder and an action-taking `Logger` protocol for Dependency Injection.
10
+
11
+ ## Simple setup, ordinary logging
12
+
13
+ Register logging during startup, then inject `Logger` into the classes that need it. `create_nyssa_logging()` registers standard error output by default, with `Information` and higher levels enabled:
14
+
15
+ ```python
16
+ from nyssa_ai_logging import create_nyssa_logging
17
+
18
+ logging = (
19
+ create_nyssa_logging("logs/service.log")
20
+ .with_rotating_json_log("logs/application.json")
21
+ .with_console()
22
+ )
23
+
24
+ logger = logging.create_logger("OrderService")
25
+ logger.info("Order {OrderId} placed", {"OrderId": 1042}, operation="OrderPlaced")
26
+ ```
27
+
28
+ ## The JSON file
29
+
30
+ The log file contains one complete JSON object per line matching the cross-language record schema:
31
+
32
+ ```json
33
+ {
34
+ "Timestamp": "2026-10-12T14:23:45.123456+00:00",
35
+ "Level": "Information",
36
+ "Message": "Order 1042 placed",
37
+ "Module": "OrderService",
38
+ "Operation": "OrderPlaced",
39
+ "Exception": null,
40
+ "AdditionalFields": {
41
+ "OrderId": 1042
42
+ }
43
+ }
44
+ ```
45
+
46
+ ## Rotation and retention
47
+
48
+ | Setting | Behavior |
49
+ | --- | --- |
50
+ | Rotation threshold | 10 MiB, or 10,485,760 bytes. |
51
+ | Backup count | Up to five numbered backups beside the active file. |
52
+ | Backup names | `service.log.1` is the newest; `service.log.5` is the oldest. |
53
+ | Age limit | Backups older than 30 days are pruned at startup and during rotation. |
@@ -0,0 +1,66 @@
1
+ [build-system]
2
+ requires = ["hatchling"]
3
+ build-backend = "hatchling.build"
4
+
5
+ [project]
6
+ name = "nyssa-ai-logging"
7
+ version = "0.2.2"
8
+ description = "Lightweight operational logging destinations with rotating JSON files and clean protocol streams matching NyssaAI.Logging."
9
+ readme = "README.md"
10
+ requires-python = ">=3.12"
11
+ license = { text = "Apache-2.0" }
12
+ authors = [
13
+ { name = "NyssaAI", email = "oss@nyssa.ai" }
14
+ ]
15
+ keywords = [
16
+ "logging",
17
+ "jsonl",
18
+ "rotating",
19
+ "stderr",
20
+ "structured-logging"
21
+ ]
22
+ classifiers = [
23
+ "Development Status :: 4 - Beta",
24
+ "Intended Audience :: Developers",
25
+ "License :: OSI Approved :: Apache Software License",
26
+ "Operating System :: OS Independent",
27
+ "Programming Language :: Python :: 3",
28
+ "Programming Language :: Python :: 3.12",
29
+ "Programming Language :: Python :: 3.13",
30
+ "Topic :: System :: Logging",
31
+ "Topic :: Software Development :: Libraries :: Python Modules",
32
+ "Typing :: Typed",
33
+ ]
34
+ dependencies = []
35
+
36
+ [project.optional-dependencies]
37
+ dev = [
38
+ "pytest>=8.0.0",
39
+ "pytest-cov>=4.0.0",
40
+ "mypy>=1.9.0",
41
+ "ruff>=0.3.0",
42
+ ]
43
+
44
+ [project.urls]
45
+ Homepage = "https://github.com/NyssaAI/NyssaAI.Shared"
46
+ Repository = "https://github.com/NyssaAI/NyssaAI.Shared.git"
47
+ Issues = "https://github.com/NyssaAI/NyssaAI.Shared/issues"
48
+
49
+ [tool.hatch.build.targets.wheel]
50
+ packages = ["src/nyssa_ai_logging"]
51
+
52
+ [tool.pytest.ini_options]
53
+ testpaths = ["test/unit", "test/use_case"]
54
+ python_files = ["test_*.py", "*_test.py"]
55
+ pythonpath = ["src", "."]
56
+
57
+ [tool.mypy]
58
+ strict = true
59
+ python_version = "3.12"
60
+ warn_return_any = true
61
+ warn_unused_configs = true
62
+ mypy_path = "src"
63
+
64
+ [tool.ruff]
65
+ line-length = 120
66
+ target-version = "py312"
@@ -0,0 +1,48 @@
1
+ # Responsibility: Export the public surface of nyssa_ai_logging.
2
+ # Public interface:
3
+ # (none)
4
+ """NyssaAI Logging for Python."""
5
+
6
+ from nyssa_ai_logging.log_level import LogLevel, is_log_level_enabled, parse_log_level
7
+ from nyssa_ai_logging.logger import Logger
8
+ from nyssa_ai_logging.logger_provider import LoggerProvider
9
+ from nyssa_ai_logging.configuration.composite_logger import CompositeLogger
10
+ from nyssa_ai_logging.configuration.logging_builder import LoggingBuilder
11
+ from nyssa_ai_logging.configuration.create_nyssa_logging import create_nyssa_logging
12
+ from nyssa_ai_logging.file_logging.clock import Clock
13
+ from nyssa_ai_logging.file_logging.system_clock import SystemClock
14
+ from nyssa_ai_logging.file_logging.log_record import LogRecord
15
+ from nyssa_ai_logging.file_logging.log_record_serializer import LogRecordSerializer
16
+ from nyssa_ai_logging.file_logging.log_file import LogFile
17
+ from nyssa_ai_logging.file_logging.owner_only_access import OwnerOnlyAccess
18
+ from nyssa_ai_logging.file_logging.stderr_diagnostics import StderrDiagnostics
19
+ from nyssa_ai_logging.file_logging.json_file_logger import JsonFileLogger
20
+ from nyssa_ai_logging.file_logging.json_file_logger_provider import JsonFileLoggerProvider
21
+ from nyssa_ai_logging.console_logging.stderr_output_logger import StderrOutputLogger
22
+ from nyssa_ai_logging.console_logging.stderr_output_logger_provider import StderrOutputLoggerProvider
23
+ from nyssa_ai_logging.console_logging.console_output_logger import ConsoleOutputLogger
24
+ from nyssa_ai_logging.console_logging.console_output_logger_provider import ConsoleOutputLoggerProvider
25
+
26
+ __all__ = [
27
+ "Clock",
28
+ "CompositeLogger",
29
+ "ConsoleOutputLogger",
30
+ "ConsoleOutputLoggerProvider",
31
+ "JsonFileLogger",
32
+ "JsonFileLoggerProvider",
33
+ "LogFile",
34
+ "LogLevel",
35
+ "LogRecord",
36
+ "LogRecordSerializer",
37
+ "Logger",
38
+ "LoggerProvider",
39
+ "LoggingBuilder",
40
+ "OwnerOnlyAccess",
41
+ "StderrDiagnostics",
42
+ "StderrOutputLogger",
43
+ "StderrOutputLoggerProvider",
44
+ "SystemClock",
45
+ "create_nyssa_logging",
46
+ "is_log_level_enabled",
47
+ "parse_log_level",
48
+ ]
@@ -0,0 +1,4 @@
1
+ # Responsibility: Internal package initialization for logging configuration.
2
+ # Public interface:
3
+ # (none)
4
+ """Configuration module initialization."""
@@ -0,0 +1,93 @@
1
+ # Responsibility: Dispatch structured log messages to multiple underlying logger instances.
2
+ # Public interface:
3
+ # `-- CompositeLogger
4
+ # +-- critical(message, error, fields, operation)
5
+ # +-- debug(message, fields, operation)
6
+ # +-- error(message, error, fields, operation)
7
+ # +-- info(message, fields, operation)
8
+ # +-- is_enabled(level)
9
+ # +-- new(loggers)
10
+ # +-- trace(message, fields, operation)
11
+ # `-- warn(message, fields, operation)
12
+ """Composite logger multiplexing calls to multiple child loggers."""
13
+
14
+ from __future__ import annotations
15
+
16
+ from collections.abc import Mapping, Sequence
17
+
18
+ from nyssa_ai_logging.log_level import LogLevel
19
+ from nyssa_ai_logging.logger import Logger
20
+
21
+
22
+ class CompositeLogger:
23
+ """Dispatches log events to multiple configured loggers."""
24
+
25
+ def __init__(self, loggers: Sequence[Logger]) -> None:
26
+ """Initialize with a sequence of child loggers."""
27
+ self._loggers = tuple(loggers)
28
+
29
+ def is_enabled(self, level: LogLevel) -> bool:
30
+ """Return True when any child logger is enabled for level."""
31
+ return any(logger.is_enabled(level) for logger in self._loggers)
32
+
33
+ def trace(
34
+ self,
35
+ message: str,
36
+ fields: Mapping[str, object] | None = None,
37
+ operation: str = "",
38
+ ) -> None:
39
+ """Forward trace record to all child loggers."""
40
+ for logger in self._loggers:
41
+ logger.trace(message, fields, operation)
42
+
43
+ def debug(
44
+ self,
45
+ message: str,
46
+ fields: Mapping[str, object] | None = None,
47
+ operation: str = "",
48
+ ) -> None:
49
+ """Forward debug record to all child loggers."""
50
+ for logger in self._loggers:
51
+ logger.debug(message, fields, operation)
52
+
53
+ def info(
54
+ self,
55
+ message: str,
56
+ fields: Mapping[str, object] | None = None,
57
+ operation: str = "",
58
+ ) -> None:
59
+ """Forward info record to all child loggers."""
60
+ for logger in self._loggers:
61
+ logger.info(message, fields, operation)
62
+
63
+ def warn(
64
+ self,
65
+ message: str,
66
+ fields: Mapping[str, object] | None = None,
67
+ operation: str = "",
68
+ ) -> None:
69
+ """Forward warn record to all child loggers."""
70
+ for logger in self._loggers:
71
+ logger.warn(message, fields, operation)
72
+
73
+ def error(
74
+ self,
75
+ message: str,
76
+ error: BaseException | None = None,
77
+ fields: Mapping[str, object] | None = None,
78
+ operation: str = "",
79
+ ) -> None:
80
+ """Forward error record to all child loggers."""
81
+ for logger in self._loggers:
82
+ logger.error(message, error, fields, operation)
83
+
84
+ def critical(
85
+ self,
86
+ message: str,
87
+ error: BaseException | None = None,
88
+ fields: Mapping[str, object] | None = None,
89
+ operation: str = "",
90
+ ) -> None:
91
+ """Forward critical record to all child loggers."""
92
+ for logger in self._loggers:
93
+ logger.critical(message, error, fields, operation)
@@ -0,0 +1,23 @@
1
+ # Responsibility: Factory function configuring default Nyssa logging with standard error and optional JSON file.
2
+ # Public interface:
3
+ # `-- create_nyssa_logging(file_path, minimum_level, clock)
4
+ """Factory function for single-call Nyssa logging setup."""
5
+
6
+ from __future__ import annotations
7
+
8
+ from nyssa_ai_logging.log_level import LogLevel
9
+ from nyssa_ai_logging.file_logging.clock import Clock
10
+ from nyssa_ai_logging.configuration.logging_builder import LoggingBuilder
11
+
12
+
13
+ def create_nyssa_logging(
14
+ file_path: str | None = None,
15
+ minimum_level: LogLevel = "Information",
16
+ clock: Clock | None = None,
17
+ ) -> LoggingBuilder:
18
+ """Configure default Nyssa logging to standard error and optional JSON file."""
19
+ builder = LoggingBuilder(minimum_level, clock)
20
+ builder.with_stderr(minimum_level)
21
+ if file_path:
22
+ builder.add_nyssa_json_file(file_path, minimum_level)
23
+ return builder
@@ -0,0 +1,124 @@
1
+ # Responsibility: Fluent builder for configuring logging destinations and creating loggers.
2
+ # Public interface:
3
+ # `-- LoggingBuilder
4
+ # +-- add_nyssa_json_file(file_path, minimum_level)
5
+ # +-- add_provider(provider)
6
+ # +-- create_logger(category_name)
7
+ # +-- dispose()
8
+ # +-- new(minimum_level, clock)
9
+ # +-- set_minimum_level(level)
10
+ # +-- with_console(minimum_level)
11
+ # +-- with_rotating_json_log(file_path, minimum_level)
12
+ # `-- with_stderr(minimum_level)
13
+ """Fluent builder for registering destinations and resolving loggers."""
14
+
15
+ from __future__ import annotations
16
+
17
+ import os
18
+ from types import TracebackType
19
+
20
+ from nyssa_ai_logging.log_level import LogLevel
21
+ from nyssa_ai_logging.logger import Logger
22
+ from nyssa_ai_logging.logger_provider import LoggerProvider
23
+ from nyssa_ai_logging.file_logging.clock import Clock
24
+ from nyssa_ai_logging.file_logging.system_clock import SystemClock
25
+ from nyssa_ai_logging.file_logging.json_file_logger_provider import JsonFileLoggerProvider
26
+ from nyssa_ai_logging.console_logging.stderr_output_logger_provider import StderrOutputLoggerProvider
27
+ from nyssa_ai_logging.console_logging.console_output_logger_provider import ConsoleOutputLoggerProvider
28
+ from nyssa_ai_logging.configuration.composite_logger import CompositeLogger
29
+
30
+
31
+ class LoggingBuilder:
32
+ """Configures logging destinations and creates category-scoped loggers."""
33
+
34
+ def __init__(
35
+ self,
36
+ minimum_level: LogLevel = "Information",
37
+ clock: Clock | None = None,
38
+ ) -> None:
39
+ """Initialize with minimum log level and optional clock."""
40
+ self._minimum_level = minimum_level
41
+ self._clock = clock if clock is not None else SystemClock()
42
+ self._providers: list[LoggerProvider] = []
43
+ self._has_stderr = False
44
+ self._has_console = False
45
+ self._disposed = False
46
+
47
+ def set_minimum_level(self, level: LogLevel) -> LoggingBuilder:
48
+ """Set the default minimum level for subsequent providers."""
49
+ self._minimum_level = level
50
+ return self
51
+
52
+ def with_stderr(self, minimum_level: LogLevel | None = None) -> LoggingBuilder:
53
+ """Add standard error output if not already present."""
54
+ if not self._has_stderr:
55
+ self._has_stderr = True
56
+ eff_level = minimum_level if minimum_level is not None else self._minimum_level
57
+ self._providers.append(StderrOutputLoggerProvider(eff_level))
58
+ return self
59
+
60
+ def with_console(self, minimum_level: LogLevel | None = None) -> LoggingBuilder:
61
+ """Add standard output if not already present."""
62
+ if not self._has_console:
63
+ self._has_console = True
64
+ eff_level = minimum_level if minimum_level is not None else self._minimum_level
65
+ self._providers.append(ConsoleOutputLoggerProvider(eff_level))
66
+ return self
67
+
68
+ def with_rotating_json_log(
69
+ self,
70
+ file_path: str,
71
+ minimum_level: LogLevel | None = None,
72
+ ) -> LoggingBuilder:
73
+ """Add a rotating JSON file whose name starts with its opening date in UTC."""
74
+ directory = os.path.dirname(file_path)
75
+ effective_dir = directory if directory and directory != "." else os.getcwd()
76
+ date_str = self._clock.now().strftime("%Y.%m.%d")
77
+ base_name = os.path.basename(file_path)
78
+ destination = os.path.join(effective_dir, f"{date_str}-{base_name}")
79
+ return self.add_nyssa_json_file(destination, minimum_level)
80
+
81
+ def add_nyssa_json_file(
82
+ self,
83
+ file_path: str,
84
+ minimum_level: LogLevel | None = None,
85
+ ) -> LoggingBuilder:
86
+ """Add a structured rotating JSON file destination at the exact supplied path."""
87
+ eff_level = minimum_level if minimum_level is not None else self._minimum_level
88
+ provider = JsonFileLoggerProvider.try_create(
89
+ file_path, self._clock, eff_level
90
+ )
91
+ if provider is not None:
92
+ self._providers.append(provider)
93
+ return self
94
+
95
+ def add_provider(self, provider: LoggerProvider) -> LoggingBuilder:
96
+ """Add an arbitrary LoggerProvider."""
97
+ self._providers.append(provider)
98
+ return self
99
+
100
+ def create_logger(self, category_name: str) -> Logger:
101
+ """Resolve a composite Logger for category_name across all registered providers."""
102
+ loggers = [p.create_logger(category_name) for p in self._providers]
103
+ return CompositeLogger(loggers)
104
+
105
+ def dispose(self) -> None:
106
+ """Dispose all registered providers and flush pending writes."""
107
+ if self._disposed:
108
+ return
109
+ self._disposed = True
110
+ for provider in self._providers:
111
+ provider.dispose()
112
+
113
+ def __enter__(self) -> LoggingBuilder:
114
+ """Enter context manager."""
115
+ return self
116
+
117
+ def __exit__(
118
+ self,
119
+ exc_type: type[BaseException] | None,
120
+ exc_val: BaseException | None,
121
+ exc_tb: TracebackType | None,
122
+ ) -> None:
123
+ """Exit context manager and dispose providers."""
124
+ self.dispose()
@@ -0,0 +1,4 @@
1
+ # Responsibility: Internal package initialization for console logging destinations.
2
+ # Public interface:
3
+ # (none)
4
+ """Console logging module initialization."""