Enhancement: Add environment variable to set the kpm output information level. #586
+102
−2
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.
1. Does this PR affect any open issues?(Y/N) and add issue references (e.g. "fix #123", "re #123".):
fix #124
2. What is the scope of this PR (e.g. component or file name):
3. Provide a description of the PR(e.g. more details, effects, motivations or doc link):
Description:
This PR improves the logging mechanism in the KPM package manager by replacing the use of os.Stdin for logging with a custom LogWriter implementation. The changes aim to provide better control over log levels (info and debug) and enhance the maintainability and flexibility of the logging system.
Key Changes:
Custom LogWriter:
Introduced a new LogWriter struct that implements the io.Writer interface.
Supports log levels (info and debug), configurable via the environment variable KPM_LOG_LEVEL.
Defaults to info level if no environment variable is set.
Enhanced KpmClient Logging:
Replaced os.Stdin with logger.NewLogWriter() for the logWriter in KpmClient.
Added a WriteLog method in KpmClient to standardize logging throughout the application.
Modified main.go Initialization:
Updated the main.go file to use kpmcli.WriteLog for logging initialization errors instead of reporter.Fatal.
Event Logging:
Added the ability to log diagnostic messages via the KpmEvent struct, which integrates with the updated KpmClient.
Why These Changes Were Made:
The previous logging mechanism lacked flexibility and log-level filtering, leading to noisy logs in production environments.
The new LogWriter provides a clean and extensible logging interface, making it easier to maintain and debug.
Replacing reporter.Fatal() ensures uniform logging and simplifies the codebase by consolidating logging logic into KpmClient.
How It Works:
The log level can be set via the KPM_LOG_LEVEL environment variable (info or debug).
Logs are written to stdout with appropriate prefixes ([INFO] or [DEBUG]).
Critical errors can still be handled by extending WriteLog to support fatal errors if necessary.
Future Improvements:
Add more log levels (e.g., warn, error, fatal) for greater logging granularity.
Consider reintroducing reporter.Fatal() or adding equivalent behavior for handling critical errors.
Testing:
Verified logs are written to stdout with appropriate prefixes and log levels.
Tested info and debug logging in different environments by setting the KPM_LOG_LEVEL variable.
Ensured no runtime errors during client initialization and command execution.
Impact:
Improves log readability and debugging.
Provides a scalable foundation for future enhancements to logging functionality.
4. Are there any breaking changes?(Y/N) and describe the breaking changes(e.g. more details, motivations or doc link):
5. Are there test cases for these changes?(Y/N) select and add more details, references or doc links: