Skip to content

Logging improvements#99

Merged
thiagoralves merged 11 commits into
mainfrom
development
Jan 30, 2026
Merged

Logging improvements#99
thiagoralves merged 11 commits into
mainfrom
development

Conversation

@thiagoralves
Copy link
Copy Markdown
Contributor

This pull request refactors the plugin driver codebase to use a unified logging system (log_info, log_debug, log_error, log_warn) instead of direct printf/fprintf/puts statements for status and error messages. This improves consistency, makes log levels explicit, and centralizes output formatting and control.

Logging system integration:

Codebase maintenance:

  • Added #include "../plc_app/utils/log.h" to plugin_config.c to enable use of the logging macros.

This change centralizes and standardizes logging, making it easier to control log output and maintain the codebase.

claude and others added 11 commits January 30, 2026 01:34
By default, debug-level logs are now disabled (no terminal output, no
Unix socket transmission, no REST API exposure). This reduces noise in
production environments.

When --print-debug is passed to start_openplc.sh, it propagates to both
the Python webserver and C runtime, enabling debug logs across all
components:

- C runtime: Only sets LOG_LEVEL_DEBUG when flag is passed
- Python webserver: Sets handler levels to INFO by default, DEBUG with flag
- Plugins: Automatically respect the C runtime log level

https://claude.ai/code/session_01YaVx8wb6vyPSnyfYW6QYMP
Instead of parsing specific arguments in the bash script, forward all
command line arguments directly to the Python webserver. This is more
generic and allows future arguments to work without script modifications.

https://claude.ai/code/session_01YaVx8wb6vyPSnyfYW6QYMP
- C runtime: Use else-if and early break when both flags are found
  to avoid redundant loop iterations
- Python logger: Always update handler levels consistently regardless
  of whether handlers already exist, ensuring config changes are
  properly reflected

https://claude.ai/code/session_01YaVx8wb6vyPSnyfYW6QYMP
…PyoXM

feat: Add --print-debug flag to control debug log output
Replace JSON output with human-readable format for stdout logs:
[YYYY-MM-DD HH:MM:SS] [LEVEL] message

- Add HumanReadableFormatter class to formatter.py
- Use HumanReadableFormatter for StreamHandler (stdout)
- Keep JsonFormatter for BufferHandler (API responses)
- Handle both Python-native logs and C runtime JSON logs
- Convert Unix timestamps to human-readable datetime

This is Phase 1 of the logging normalization effort.
API endpoint /api/runtime-logs still returns JSON format.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Phase 2 of logging normalization - convert direct printf/fprintf
calls in C code to use the centralized logging system.

Files modified:
- plugin_driver.c: Convert 73 printf/fprintf calls to log_info/log_error/log_warn/log_debug
- plugin_config.c: Convert 2 printf calls to log_debug
- plugin_logger.c: Convert init warnings to log_warn, format fallback output
- journal_buffer.c: Convert 2 fprintf calls to log_error
- plc_main.c: Format early init error with timestamp
- watchdog.c: Format critical error with timestamp

Fallback logging (when central logging unavailable) now uses
human-readable format: [YYYY-MM-DD HH:MM:SS] [LEVEL] message

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
plugin_logger.c is compiled into native plugins, not the main runtime.
Using log_warn/log_error directly causes undefined symbol errors when
loading plugins.

Replace with PLUGIN_LOGGER_STDERR macro that uses fprintf with the
human-readable timestamp format.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Convert print() statements in webserver Python files to use the
centralized logging system.

Files modified:
- plugin_config_model.py: 6 print -> logger.info/error/warning
- credentials.py: 10 print -> logger.info/error/warning
- config.py: 2 print -> logger.info/error
- app.py: 2 print -> logger.info

Note: Python plugin files (modbus_master_memory.py) need a
dedicated logging solution similar to opcua_logging.py and
are deferred to Phase 4.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Update Python plugin logger fallback print statements to use the
standard timestamp format for consistency:
- shared/plugin_logger.py: Add timestamp to _fallback_print()
- opcua/opcua_logging.py: Add timestamps to info/warn/error/debug fallbacks

Format: [YYYY-MM-DD HH:MM:SS] [LEVEL] [PREFIX] message

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Move diagnostic and verbose logging from INFO to DEBUG level:
- opcua_security.py: Certificate details, security setup debug blocks
- user_manager.py: Per-connection authentication details
- address_space.py: Node creation details
- server.py: Endpoint verification, setup progress
- plugin.py: Internal initialization steps
- callbacks.py: Permission callback registration details
- synchronization.py: Sync manager internal state
- config.py: Configuration parsing details
- opcua_memory.py: Metadata caching details

Keep as INFO: Important status messages (server started/stopped,
plugin initialized, security initialized, sync loop status)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Normalize log output format for human-readable stdout
@thiagoralves thiagoralves merged commit 32b76b9 into main Jan 30, 2026
0 of 2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants