generated from intersystems-community/iris-interoperability-template
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add logging documentation and integrate Python logging with IRIS; imp…
…lement LogManager and update logging methods in _Common class
- Loading branch information
1 parent
b50159c
commit b463aae
Showing
5 changed files
with
223 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# Logging | ||
|
||
InterSystems IRIS Interoperability framework implements its own logging system. The Python API provides a way to use Python's logging module integrated with IRIS logging. | ||
|
||
## Basic Usage | ||
|
||
The logging system is available through the component base class. You can access it via the `logger` property or use the convenience methods: | ||
|
||
```python | ||
def on_init(self): | ||
# Using convenience methods | ||
self.log_info("Component initialized") | ||
self.log_error("An error occurred") | ||
self.log_warning("Warning message") | ||
self.log_alert("Critical alert") | ||
self.trace("Debug trace message") | ||
|
||
# Using logger property | ||
self.logger.info("Info via logger") | ||
self.logger.error("Error via logger") | ||
``` | ||
|
||
## Console Logging | ||
|
||
You can direct logs to the console instead of IRIS in two ways: | ||
|
||
1. Set the component-wide setting: | ||
```python | ||
def on_init(self): | ||
self.log_to_console = True | ||
self.log_info("This will go to console") | ||
``` | ||
|
||
2. Per-message console logging: | ||
```python | ||
def on_message(self, request): | ||
# Log specific message to console | ||
self.log_info("Debug info", to_console=True) | ||
|
||
# Other logs still go to IRIS | ||
self.log_info("Production info") | ||
``` | ||
|
||
## Log Levels | ||
|
||
The following log levels are available: | ||
|
||
- `trace()` - Debug level logging (maps to IRIS LogTrace) | ||
- `log_info()` - Information messages (maps to IRIS LogInfo) | ||
- `log_warning()` - Warning messages (maps to IRIS LogWarning) | ||
- `log_error()` - Error messages (maps to IRIS LogError) | ||
- `log_alert()` - Critical/Alert messages (maps to IRIS LogAlert) | ||
- `log_assert()` - Assert messages (maps to IRIS LogAssert) | ||
|
||
## Integration with IRIS | ||
|
||
The Python logging is automatically mapped to the appropriate IRIS logging methods: | ||
|
||
- Python `DEBUG` → IRIS `LogTrace` | ||
- Python `INFO` → IRIS `LogInfo` | ||
- Python `WARNING` → IRIS `LogWarning` | ||
- Python `ERROR` → IRIS `LogError` | ||
- Python `CRITICAL` → IRIS `LogAlert` | ||
|
||
## Legacy Methods | ||
|
||
The following methods are deprecated but maintained for backwards compatibility: | ||
|
||
- `LOGINFO()` - Use `log_info()` instead | ||
- `LOGALERT()` - Use `log_alert()` instead | ||
- `LOGWARNING()` - Use `log_warning()` instead | ||
- `LOGERROR()` - Use `log_error()` instead | ||
- `LOGASSERT()` - Use `log_assert()` instead |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import iris | ||
import logging | ||
from typing import Optional, Tuple | ||
|
||
class LogManager: | ||
"""Manages logging integration between Python's logging module and IRIS.""" | ||
|
||
@staticmethod | ||
def get_logger(class_name: str, method_name: Optional[str] = None, console: bool = False) -> logging.Logger: | ||
"""Get a logger instance configured for IRIS integration. | ||
Args: | ||
class_name: Name of the class logging the message | ||
method_name: Optional name of the method logging the message | ||
console: If True, log to the console instead of IRIS | ||
Returns: | ||
Logger instance configured for IRIS integration | ||
""" | ||
logger = logging.getLogger(f"{class_name}.{method_name}" if method_name else class_name) | ||
|
||
# Only add handler if none exists | ||
if not logger.handlers: | ||
handler = IRISLogHandler(class_name, method_name, console) | ||
formatter = logging.Formatter('%(message)s') | ||
handler.setFormatter(formatter) | ||
logger.addHandler(handler) | ||
# Set the log level to the lowest level to ensure all messages are sent to IRIS | ||
logger.setLevel(logging.DEBUG) | ||
|
||
return logger | ||
|
||
class IRISLogHandler(logging.Handler): | ||
"""Custom logging handler that routes Python logs to IRIS logging system.""" | ||
|
||
def __init__(self, class_name: str, method_name: Optional[str] = None, console: bool = False): | ||
"""Initialize the handler with context information. | ||
Args: | ||
class_name: Name of the class logging the message | ||
method_name: Optional name of the method logging the message | ||
console: If True, log to the console instead of IRIS | ||
""" | ||
super().__init__() | ||
self.class_name = class_name | ||
self.method_name = method_name | ||
self.console = console | ||
|
||
def format(self, record: logging.LogRecord) -> str: | ||
"""Format the log record into a string. | ||
Args: | ||
record: The logging record to format | ||
Returns: | ||
Formatted log message | ||
""" | ||
if self.console: | ||
return f"{record}" | ||
return record.getMessage() | ||
|
||
def emit(self, record: logging.LogRecord) -> None: | ||
"""Route the log record to appropriate IRIS logging method. | ||
Args: | ||
record: The logging record to emit | ||
""" | ||
# Map Python logging levels to IRIS logging methods | ||
level_map = { | ||
logging.DEBUG: iris.cls("Ens.Util.Log").LogTrace, | ||
logging.INFO: iris.cls("Ens.Util.Log").LogInfo, | ||
logging.WARNING: iris.cls("Ens.Util.Log").LogWarning, | ||
logging.ERROR: iris.cls("Ens.Util.Log").LogError, | ||
logging.CRITICAL: iris.cls("Ens.Util.Log").LogAlert, | ||
} | ||
|
||
log_func = level_map.get(record.levelno, iris.cls("Ens.Util.Log").LogInfo) | ||
if self.console or (hasattr(record, "to_console") and record.to_console): | ||
iris.cls("%SYS.System").WriteToConsoleLog(self.format(record),0,0,"IoP.Log") | ||
else: | ||
log_func(self.class_name, self.method_name, self.format(record)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters