-
-
Notifications
You must be signed in to change notification settings - Fork 1
Add configurable sysLogger class for performance-optimized logging #180
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
Draft
Copilot
wants to merge
4
commits into
main
Choose a base branch
from
copilot/add-configurable-logger-class
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,218 @@ | ||
| .. appdev-logging | ||
|
|
||
| .. _appdevlogging: | ||
|
|
||
| Logging System | ||
| ============== | ||
|
|
||
| The *x0-framework* provides a configurable logging mechanism through the ``sysLogger`` class. | ||
| This logging system replaces direct ``console.debug()`` calls and allows fine-grained control | ||
| over log output based on configurable debug levels. | ||
|
|
||
| Overview | ||
| -------- | ||
|
|
||
| The logging system is designed to: | ||
|
|
||
| * Reduce performance impact of debug statements in production | ||
| * Provide configurable log levels from the PostgreSQL backend | ||
| * Support standard logging levels (ERROR, WARN, INFO, DEBUG) | ||
| * Maintain backward compatibility with existing code | ||
|
|
||
| Configuration | ||
| ------------- | ||
|
|
||
| The logging level is configured in the PostgreSQL database via the ``debug_level`` configuration: | ||
|
|
||
| .. code-block:: sql | ||
|
|
||
| INSERT INTO system.config (config_group, "value") | ||
| VALUES ('debug_level', '10'); | ||
|
|
||
| Log Levels | ||
| ---------- | ||
|
|
||
| The following log levels are available: | ||
|
|
||
| =============== ======= ===================================================== | ||
| Level Name Value Description | ||
| =============== ======= ===================================================== | ||
| LOG_LEVEL_NONE 0 No logging (production) | ||
| LOG_LEVEL_ERROR 1 Only error messages | ||
| LOG_LEVEL_WARN 5 Warnings and errors | ||
| LOG_LEVEL_INFO 8 Informational messages, warnings, and errors | ||
| LOG_LEVEL_DEBUG 10 All messages including debug output | ||
| =============== ======= ===================================================== | ||
|
|
||
| Usage | ||
| ----- | ||
|
|
||
| The logger is accessible globally through ``sysFactory.Logger`` after initialization. | ||
|
|
||
| Basic Logging | ||
| ^^^^^^^^^^^^^ | ||
|
|
||
| .. code-block:: javascript | ||
|
|
||
| // Debug messages (level 10) | ||
| sysFactory.Logger.debug('Debug message:', objectData); | ||
|
|
||
| // Info messages (level 8) | ||
| sysFactory.Logger.info('User logged in:', userId); | ||
|
|
||
| // Warning messages (level 5) | ||
| sysFactory.Logger.warn('Deprecated function called:', functionName); | ||
|
|
||
| // Error messages (level 1) | ||
| sysFactory.Logger.error('Operation failed:', errorDetails); | ||
|
|
||
| Custom Log Levels | ||
| ^^^^^^^^^^^^^^^^^ | ||
|
|
||
| You can also use custom log levels: | ||
|
|
||
| .. code-block:: javascript | ||
|
|
||
| // Log with custom level (7) | ||
| sysFactory.Logger.log(7, 'Custom level message:', data); | ||
|
|
||
| Getting/Setting Log Level | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
|
||
| .. code-block:: javascript | ||
|
|
||
| // Get current log level | ||
| var currentLevel = sysFactory.Logger.getLogLevel(); | ||
|
|
||
| // Set log level programmatically (not recommended, use config instead) | ||
| sysFactory.Logger.setLogLevel(5); // Set to WARN level | ||
|
|
||
| Examples | ||
| -------- | ||
|
|
||
| Basic Usage | ||
| ^^^^^^^^^^^ | ||
|
|
||
| .. code-block:: javascript | ||
|
|
||
| function sysReactor() { | ||
| this.Events = new Array(); | ||
| sysFactory.Logger.debug('Reactor initialized with events array'); | ||
| } | ||
|
|
||
| sysReactor.prototype.registerEvent = function(Attributes, ProcessObject, Type) { | ||
| sysFactory.Logger.debug('registerEvent Attributes:%o ProcessObject:%o, Type:%s', | ||
| Attributes, ProcessObject, Type); | ||
| // ... rest of implementation | ||
| } | ||
|
|
||
| Conditional Logging | ||
| ^^^^^^^^^^^^^^^^^^^ | ||
|
|
||
| .. code-block:: javascript | ||
|
|
||
| function processData(data) { | ||
| try { | ||
| // Process data | ||
| sysFactory.Logger.info('Data processed successfully'); | ||
| } catch (err) { | ||
| sysFactory.Logger.error('Data processing failed:', err); | ||
| } | ||
| } | ||
|
|
||
| Migration Guide | ||
| --------------- | ||
|
|
||
| Existing ``console.debug()`` calls can be gradually migrated to use the logger: | ||
|
|
||
| Before: | ||
|
|
||
| .. code-block:: javascript | ||
|
|
||
| console.debug('TreeSimple JSONConfig:%o', Attributes); | ||
|
|
||
| After: | ||
|
|
||
| .. code-block:: javascript | ||
|
|
||
| sysFactory.Logger.debug('TreeSimple JSONConfig:%o', Attributes); | ||
|
|
||
| Benefits | ||
| -------- | ||
|
|
||
| Using the logger class provides several advantages: | ||
|
|
||
| 1. **Performance**: Logging can be completely disabled in production (level 0) | ||
| 2. **Flexibility**: Different log levels can be enabled without code changes | ||
| 3. **Consistency**: Standardized logging interface across the framework | ||
| 4. **Control**: Centralized configuration through the database | ||
|
|
||
| Best Practices | ||
| -------------- | ||
|
|
||
| 1. Use appropriate log levels: | ||
|
|
||
| * ``debug()`` for detailed debugging information | ||
| * ``info()`` for general informational messages | ||
| * ``warn()`` for warning messages about potential issues | ||
| * ``error()`` for error conditions | ||
|
|
||
| 2. Include context in log messages: | ||
|
|
||
| .. code-block:: javascript | ||
|
|
||
| // Good - includes context | ||
| sysFactory.Logger.debug('User validation failed for ID:%s', userId); | ||
|
|
||
| // Poor - lacks context | ||
| sysFactory.Logger.debug('Validation failed'); | ||
|
|
||
| 3. Use structured logging with objects: | ||
|
|
||
| .. code-block:: javascript | ||
|
|
||
| sysFactory.Logger.debug('Request data:', { | ||
| url: requestUrl, | ||
| method: requestMethod, | ||
| params: requestParams | ||
| }); | ||
|
|
||
| 4. Avoid logging sensitive information: | ||
|
|
||
| .. code-block:: javascript | ||
|
|
||
| // Avoid logging passwords, tokens, or personal data | ||
| sysFactory.Logger.debug('User logged in', { userId: user.id }); // OK | ||
| // NOT: sysFactory.Logger.debug('Login', { password: user.password }); // NEVER! | ||
|
|
||
| Technical Details | ||
| ----------------- | ||
|
|
||
| Implementation | ||
| ^^^^^^^^^^^^^^ | ||
|
|
||
| The ``sysLogger`` class is implemented in ``/www/sysLogger.js`` and provides: | ||
|
|
||
| * Constructor: ``sysLogger()`` | ||
| * Methods: | ||
|
|
||
| * ``setLogLevel(level)`` - Set the logging level | ||
| * ``getLogLevel()`` - Get the current logging level | ||
| * ``debug(...)`` - Log debug messages | ||
| * ``info(...)`` - Log info messages | ||
| * ``warn(...)`` - Log warning messages | ||
| * ``error(...)`` - Log error messages | ||
| * ``log(level, ...)`` - Log with custom level | ||
|
|
||
| Initialization | ||
| ^^^^^^^^^^^^^^ | ||
|
|
||
| The logger is initialized in ``sysInitOnLoad.js`` during application startup: | ||
|
|
||
| .. code-block:: javascript | ||
|
|
||
| sysFactory.Logger = new sysLogger(); | ||
| sysFactory.Logger.setLogLevel(sysVarDebugLevel); | ||
|
|
||
| The ``sysVarDebugLevel`` value is injected from the Python backend (``Index.py``) | ||
| based on the PostgreSQL configuration. | ||
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The documentation doesn't explain what happens if
sysVarDebugLevelis undefined or missing from the database configuration. Consider documenting the default behavior or error handling for this scenario.