Skip to content
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

[MM-19315] Replace old Logger with mlog.Logger (mattermost package) #130

Merged

Conversation

willypuzzle
Copy link
Contributor

@willypuzzle willypuzzle commented Dec 3, 2024

Summary

This PR replace the old Logger with the more modern mlog.Logger.

Ticket Link

mattermost/mattermost#14407

Release note:

Added LogFormat option, possible values are plain or json (default value: plain)

Refactored the logging system in both Android and Apple notification servers. Replaced `logger.Infof` with `logger.Info` for a more structured logging approach, using `mlog.String`, `mlog.Int`, and `mlog.Err` to provide additional context. This change enhances readability and allows for better log analysis.
The logging statements in the server and notification files have been refactored to improve readability. The changes include replacing `logger.Errorf` with `logger.Error` and using structured logging fields instead of string formatting. This makes it easier to understand the log messages and their associated data.
Switched from using Panic to Fatal in the server's error handling mechanism. This change ensures that any serious errors will cause the program to exit immediately, rather than just panicking and potentially leaving the server in an unstable state.
The logger type has been updated from *Logger to *mlog.Logger in the AndroidNotificationServer, AppleNotificationServer, and Server structures. This change also affects the NewAndroidNotificationServer, NewAppleNotificationServer functions where the logger parameter type is updated accordingly.
Refactored the ConfigPushProxy struct. Deprecated LogFileLocation, EnableConsoleLog, and EnableFileLog for backward compatibility. Introduced LoggingCfgFile and LoggingCfgJSON as replacements. No changes to other existing fields.
Removed the automatic enabling of console log when both console and file logs are disabled. Also, removed the creation and handling of log files within the configuration loading function. This simplifies the function by focusing it solely on loading configurations.
The main function has been updated to include a more robust logger initialization process. This includes error handling for the creation of a new logger, as well as its configuration. If no logging is defined, a default config (console output) is used. A separate function has been added to provide this default logging configuration. The server now also ensures that the logger is properly shut down when it's no longer needed.
Upgraded the Go version from 1.21 to 1.22 and updated various dependencies to their latest versions. This includes updates to firebase, gorilla handlers, mux, prometheus client_golang, common, golang.org/x/net and oauth2 among others. Also added new dependencies such as github.com/mattermost/mattermost/server/public and github.com/francoispqt/gojay.
@mattermost-build
Copy link
Contributor

Hello @willypuzzle,

Thanks for your pull request! A Core Committer will review your pull request soon. For code contributions, you can learn more about the review process here.

@enahum enahum requested review from agnivade and larkox December 3, 2024 18:38
@enahum enahum added the 1: Dev Review Requires review by a core commiter label Dec 3, 2024
Copy link
Contributor

@larkox larkox left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! Just a couple of comments to discuss.

server/config_push_proxy.go Outdated Show resolved Hide resolved
server/config_push_proxy.go Outdated Show resolved Hide resolved
main.go Outdated Show resolved Hide resolved
The default logging configuration has been refactored to support both console and file logging. The logic for generating the default config has been moved from main.go to a new file, server/logging.go. This change allows the application to dynamically choose between console or file logging based on the 'EnableFileLog' flag in the configuration.
@agnivade
Copy link
Member

agnivade commented Dec 4, 2024

Thank you @willypuzzle. I will be taking a look at this soon.

Copy link
Contributor

@larkox larkox left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple more comments about tests.

main.go Outdated Show resolved Hide resolved
server/logging.go Outdated Show resolved Hide resolved
Upgraded the Go version from 1.21 to 1.22 and updated various dependencies to their latest versions. Also, added new dependencies required for the project's functionality.
The Docker images for Go and Golint have been updated to newer versions. The new version of the Go image includes various performance improvements and bug fixes. Similarly, the Golint image has also been updated to include the latest linting rules and enhancements.
The logger initialization in various test files has been updated. Instead of using the NewLogger function, we are now using mlog.NewLogger(). This change affects android_notification_test.go, metrics_test.go, and server_test.go.
- Added error checks after initializing new loggers in various test functions to ensure no silent failures.
- Simplified the condition check for enabling file logs by directly using the boolean value.
Introduced a new set of unit tests for the server's logging functionality. These tests cover various aspects such as escaping double quotes, default console log configuration, file log configuration and overall logging configuration. Also added a helper function to generate random strings for testing purposes.
The logging settings have been simplified and restructured. The previous console and file log enablement flags have been removed. Instead, a new approach has been introduced where the logging configuration is now handled through a dedicated config file or directly via JSON.
A new JSON configuration file for logging has been added. This includes settings for console type output, plain formatting, and color enabling. It also specifies minimum lengths for level and message, as well as the inclusion of caller information. Different log levels from debug to panic have been defined with corresponding IDs and optional color or stacktrace attributes.
The logger initialization and configuration process has been refactored for better code organization. The logic previously in the main function is now encapsulated within a new function, NewMlogLogger, in the server package. This change also includes updates to logging tests to accommodate the new structure.
This update includes several significant changes:
- The release is compatible with all versions of Mattermost server and mobile.
- Notably, Google Cloud Messaging (GCM) service has been deprecated as of April 10, 2018, and will be removed by April 11, 2019. Migration to Firebase Cloud Messaging (FCM) is necessary.
- The old Logger system has been replaced with a new one.
- Deprecated types EnableConsoleLog, EnableFileLog, and LogFileLocation from the config have been replaced.
- Lastly, we've switched from go version 1.21 to go version 1.22 for improved performance and stability.
The README file has been updated to include a reference to the CHANGELOG. This will provide users with more detailed information about the changes made in each release.
@willypuzzle
Copy link
Contributor Author

@larkox @agnivade I made all the requested changes and improvements.

Copy link
Contributor

@larkox larkox left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work! Just two minor comments.

server/logging.go Outdated Show resolved Hide resolved
server/logging_test.go Outdated Show resolved Hide resolved
@agnivade
Copy link
Member

/update-branch

@mattermost-build
Copy link
Contributor

Error trying to update the PR.
Please do it manually.

@agnivade
Copy link
Member

@toninis - Any idea why CI is not running for this PR?

@agnivade agnivade added 2: Reviews Complete All reviewers have approved the pull request Do Not Merge Should not be merged until this label is removed and removed 1: Dev Review Requires review by a core commiter labels Dec 23, 2024
@willypuzzle
Copy link
Contributor Author

Thank you for this @willypuzzle ! There were some more changes needed. But I went ahead and did those myself since you have already spent so much time on this.

@agnivade no prob! ask the same, I'm happy to have practice with go!

@mattermost-build
Copy link
Contributor

This PR has been automatically labelled "stale" because it hasn't had recent activity.
A core team member will check in on the status of the PR to help with questions.
Thank you for your contribution!

@mattermost-build
Copy link
Contributor

This PR has been automatically labelled "stale" because it hasn't had recent activity.
A core team member will check in on the status of the PR to help with questions.
Thank you for your contribution!

@agnivade
Copy link
Member

/update-branch

@mattermost-build
Copy link
Contributor

Error trying to update the PR.
Please do it manually.

@agnivade
Copy link
Member

@willypuzzle - could you please update this branch with latest master and push again? Sorry for the delay. The person I pinged is on leave.

@willypuzzle
Copy link
Contributor Author

willypuzzle commented Jan 14, 2025

@willypuzzle - could you please update this branch with latest master and push again? Sorry for the delay. The person I pinged is on leave.

@agnivade the PR's branch is just updated
Screenshot 2025-01-14 103731

@agnivade
Copy link
Member

Can you push an empty commit? I want to re-trigger the CI flow for a fresh commit.

@agnivade
Copy link
Member

I meant just an empty commit. You can do that by git commit --allow-empty -m 'Trigger CI'. Please revert the earlier commit, and push a new one.

@willypuzzle
Copy link
Contributor Author

I meant just an empty commit. You can do that by git commit --allow-empty -m 'Trigger CI'. Please revert the earlier commit, and push a new one.

sorry, I didn't know it.

@agnivade
Copy link
Member

Thanks, I will take it from here.

@agnivade
Copy link
Member

@willypuzzle - Could you please merge with latest master and push again? This commit 93d8c3b should take care of the problem.

@agnivade agnivade removed the Do Not Merge Should not be merged until this label is removed label Jan 20, 2025
@agnivade agnivade merged commit 7d33b02 into mattermost:master Jan 20, 2025
4 of 5 checks passed
@agnivade
Copy link
Member

Thanks for your patience @willypuzzle !

@willypuzzle willypuzzle deleted the MM-19315-add-mlog-logger-as-logger branch January 20, 2025 10:04
@willypuzzle
Copy link
Contributor Author

Thanks for your patience @willypuzzle !

@agnivade Thanks to you! I always learn a lot from you all!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2: Reviews Complete All reviewers have approved the pull request Contributor Lifecycle/1:stale
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants