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

(3/3) Use Zap for structured logging rather than logrus. #115

Merged
merged 2 commits into from
Mar 18, 2023
Merged

Conversation

rtrox
Copy link
Collaborator

@rtrox rtrox commented Mar 17, 2023

NOTE: this is not actually a particularly large PR, but it depends on #110 and #113. This will be easiest to review after reviewing & merging those PRs, and rebasing this one. In the meantime, you can see just the changes in this PR by viewing this commit.

Description of the change

Per title, replaces Logrus with Zap, and utilizes structured logging for logs. This PR also adds a new flag, --log-format, which allows toggling between Console & JSON logging. Examples of what logging looks like with each:

# --log-format console (default) --log-level debug
2023-03-17T10:54:23.700-0700	DEBUG	Logger initialized
2023-03-17T10:54:23.701-0700	INFO	Starting HTTP Server	{"interface": "0.0.0.0", "port": 8081}
2023-03-17T10:54:28.453-0700	DEBUG	Request Received	{"remote_addr": "[::1]:61839", "method": "GET", "url": "/metrics"}
2023-03-17T10:54:28.454-0700	INFO	Sending HTTP request	{"url": "https://radarr.redact.com/api/v3/rootfolder"}
2023-03-17T10:54:28.454-0700	INFO	Sending HTTP request	{"url": "https://radarr.redact.com/api/v3/history"}
2023-03-17T10:54:28.454-0700	INFO	Sending HTTP request	{"url": "https://radarr.redact.com/api/v3/system/status"}
2023-03-17T10:54:28.454-0700	INFO	Sending HTTP request	{"url": "https://radarr.redact.com/api/v3/queue"}
2023-03-17T10:54:28.455-0700	INFO	Sending HTTP request	{"url": "https://radarr.redact.com/api/v3/health"}
2023-03-17T10:54:28.455-0700	INFO	Sending HTTP request	{"url": "https://radarr.redact.com/api/v3/movie"}
2023-03-17T10:54:28.640-0700	DEBUG	Request Received	{"remote_addr": "[::1]:61839", "method": "GET", "url": "/favicon.ico"}
2023-03-17T10:55:27.465-0700	INFO	Shutting down due to signal: interrupt
# --log-format json --log-format debug
{"level":"debug","ts":1679075648.1925929,"msg":"Logger initialized"}
{"level":"info","ts":1679075648.192713,"msg":"Starting HTTP Server","interface":"0.0.0.0","port":8081}
{"level":"debug","ts":1679075652.173582,"msg":"Request Received","remote_addr":"[::1]:61825","method":"GET","url":"/metrics"}
{"level":"info","ts":1679075652.173814,"msg":"Sending HTTP request","url":"https://radarr.redact.com/api/v3/history"}
{"level":"info","ts":1679075652.173852,"msg":"Sending HTTP request","url":"https://radarr.redact.com/api/v3/rootfolder"}
{"level":"info","ts":1679075652.173957,"msg":"Sending HTTP request","url":"https://radarr.redact.com/api/v3/health"}
{"level":"info","ts":1679075652.173992,"msg":"Sending HTTP request","url":"https://radarr.redact.com/api/v3/system/status"}
{"level":"info","ts":1679075652.174219,"msg":"Sending HTTP request","url":"https://radarr.redact.com/api/v3/movie"}
{"level":"info","ts":1679075652.175252,"msg":"Sending HTTP request","url":"https://radarr.redact.com/api/v3/queue"}
{"level":"debug","ts":1679075652.382421,"msg":"Request Received","remote_addr":"[::1]:61825","method":"GET","url":"/favicon.ico"}
{"level":"info","ts":1679075660.293376,"msg":"Shutting down due to signal: interrupt"}

Benefits

  • Maintenance - Logrus has entered maintenance mode, Zap is under active development
  • Parseability - Structured logging, especially with a JSON formatter, is significantly easier to parse using log aggregation tools like loki, logstash, or splunk.

Possible drawbacks

Using a new logger comes with a change in logging format.

Applicable issues

Additional information

@rtrox rtrox requested a review from onedr0p as a code owner March 17, 2023 18:04
@rtrox rtrox changed the title Use Zap for structured logging rather than logrus. (3/3) Use Zap for structured logging rather than logrus. Mar 17, 2023
@rtrox
Copy link
Collaborator Author

rtrox commented Mar 17, 2023

Good to go here, too -- validation errors were trying to log before the logger had been configured. Since these need to immediately exit, printing the error directly to stderr.

@onedr0p
Copy link
Owner

onedr0p commented Mar 18, 2023

I've reviewed this commit so anytime you're free to rebase we can merge it.

rtrox added 2 commits March 18, 2023 08:07

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
Signed-off-by: Russell Troxel <russell.troxel@segment.com>
Signed-off-by: Russell Troxel <russell.troxel@segment.com>
@rtrox
Copy link
Collaborator Author

rtrox commented Mar 18, 2023

rebased, we should be all set!

@onedr0p onedr0p merged commit 3cd02da into master Mar 18, 2023
@onedr0p
Copy link
Owner

onedr0p commented Mar 18, 2023

I'm going to test out the changes further today and release v2.0.0 soon.

@onedr0p onedr0p deleted the zap branch March 18, 2023 19:05
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.

Refactor: Switch to zap logging library
2 participants