-
Notifications
You must be signed in to change notification settings - Fork 12
feat: modernize structured logging with named payloads and labels #221
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f58cbec
feat: modernize structured logging with named payloads and labels
kevmoo 01556c8
Update pkgs/google_cloud/lib/src/logger.dart
kevmoo be996cd
Merge branch 'main' into payload_fun
brianquinlan 97f2153
review comments
kevmoo c54327c
other cleanup
kevmoo 527544b
Fix review nit, not ready to publish yet!
kevmoo 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
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
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,221 @@ | ||
| // Copyright 2022 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // https://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| import 'package:meta/meta.dart'; | ||
| import 'package:stack_trace/stack_trace.dart'; | ||
|
|
||
| /// See https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#logseverity | ||
| enum LogSeverity implements Comparable<LogSeverity> { | ||
| defaultSeverity._(0, 'DEFAULT'), | ||
| debug._(100, 'DEBUG'), | ||
| info._(200, 'INFO'), | ||
| notice._(300, 'NOTICE'), | ||
| warning._(400, 'WARNING'), | ||
| error._(500, 'ERROR'), | ||
| critical._(600, 'CRITICAL'), | ||
| alert._(700, 'ALERT'), | ||
| emergency._(800, 'EMERGENCY'); | ||
|
|
||
| final int value; | ||
| final String name; | ||
|
|
||
| const LogSeverity._(this.value, this.name); | ||
|
|
||
| @override | ||
| int compareTo(LogSeverity other) => value.compareTo(other.value); | ||
|
|
||
| bool operator <(LogSeverity other) => value < other.value; | ||
|
|
||
| bool operator <=(LogSeverity other) => value <= other.value; | ||
|
|
||
| bool operator >(LogSeverity other) => value > other.value; | ||
|
|
||
| bool operator >=(LogSeverity other) => value >= other.value; | ||
|
|
||
| @override | ||
| String toString() => 'LogSeverity $name ($value)'; | ||
|
|
||
| String toJson() => name; | ||
| } | ||
|
|
||
| /// Allows logging at a specified severity. | ||
| /// | ||
| /// Compatible with the | ||
| /// [log severities](https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#logseverity) | ||
| /// supported by Google Cloud. | ||
| abstract base class CloudLogger { | ||
| /// Const constructor for subclasses. | ||
| const CloudLogger(); | ||
|
|
||
| const factory CloudLogger.defaultLogger() = _DefaultLogger; | ||
|
|
||
| /// Logs [message] at the given [severity]. | ||
kevmoo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| void log( | ||
| Object message, | ||
| LogSeverity severity, { | ||
| Map<String, Object?>? payload, | ||
| Map<String, String>? labels, | ||
| StackTrace? stackTrace, | ||
| }); | ||
|
|
||
| /// Logs [message] at [LogSeverity.debug] severity. | ||
| void debug( | ||
| Object message, { | ||
| Map<String, Object?>? payload, | ||
| Map<String, String>? labels, | ||
| StackTrace? stackTrace, | ||
| }) => log( | ||
| message, | ||
| LogSeverity.debug, | ||
| payload: payload, | ||
| labels: labels, | ||
| stackTrace: stackTrace, | ||
| ); | ||
|
|
||
| /// Logs [message] at [LogSeverity.info] severity. | ||
| void info( | ||
| Object message, { | ||
| Map<String, Object?>? payload, | ||
| Map<String, String>? labels, | ||
| StackTrace? stackTrace, | ||
| }) => log( | ||
| message, | ||
| LogSeverity.info, | ||
| payload: payload, | ||
| labels: labels, | ||
| stackTrace: stackTrace, | ||
| ); | ||
|
|
||
| /// Logs [message] at [LogSeverity.notice] severity. | ||
| void notice( | ||
| Object message, { | ||
| Map<String, Object?>? payload, | ||
| Map<String, String>? labels, | ||
| StackTrace? stackTrace, | ||
| }) => log( | ||
| message, | ||
| LogSeverity.notice, | ||
| payload: payload, | ||
| labels: labels, | ||
| stackTrace: stackTrace, | ||
| ); | ||
|
|
||
| /// Logs [message] at [LogSeverity.warning] severity. | ||
| void warning( | ||
| Object message, { | ||
| Map<String, Object?>? payload, | ||
| Map<String, String>? labels, | ||
| StackTrace? stackTrace, | ||
| }) => log( | ||
| message, | ||
| LogSeverity.warning, | ||
| payload: payload, | ||
| labels: labels, | ||
| stackTrace: stackTrace, | ||
| ); | ||
|
|
||
| /// Logs [message] at [LogSeverity.error] severity. | ||
| void error( | ||
| Object message, { | ||
| Map<String, Object?>? payload, | ||
| Map<String, String>? labels, | ||
| StackTrace? stackTrace, | ||
| }) => log( | ||
| message, | ||
| LogSeverity.error, | ||
| payload: payload, | ||
| labels: labels, | ||
| stackTrace: stackTrace, | ||
| ); | ||
|
|
||
| /// Logs [message] at [LogSeverity.critical] severity. | ||
| void critical( | ||
| Object message, { | ||
| Map<String, Object?>? payload, | ||
| Map<String, String>? labels, | ||
| StackTrace? stackTrace, | ||
| }) => log( | ||
| message, | ||
| LogSeverity.critical, | ||
| payload: payload, | ||
| labels: labels, | ||
| stackTrace: stackTrace, | ||
| ); | ||
|
|
||
| /// Logs [message] at [LogSeverity.alert] severity. | ||
| void alert( | ||
| Object message, { | ||
| Map<String, Object?>? payload, | ||
| Map<String, String>? labels, | ||
| StackTrace? stackTrace, | ||
| }) => log( | ||
| message, | ||
| LogSeverity.alert, | ||
| payload: payload, | ||
| labels: labels, | ||
| stackTrace: stackTrace, | ||
| ); | ||
|
|
||
| /// Logs [message] at [LogSeverity.emergency] severity. | ||
| void emergency( | ||
| Object message, { | ||
| Map<String, Object?>? payload, | ||
| Map<String, String>? labels, | ||
| StackTrace? stackTrace, | ||
| }) => log( | ||
| message, | ||
| LogSeverity.emergency, | ||
| payload: payload, | ||
| labels: labels, | ||
| stackTrace: stackTrace, | ||
| ); | ||
| } | ||
|
|
||
| /// A [CloudLogger] that prints messages normally. | ||
kevmoo marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| /// | ||
| /// Any message that's not [LogSeverity.defaultSeverity] is prefixed by the | ||
| /// [LogSeverity] name. | ||
| final class _DefaultLogger extends CloudLogger { | ||
| /// Const constructor. | ||
| const _DefaultLogger(); | ||
|
|
||
| @override | ||
| void log( | ||
| Object message, | ||
| LogSeverity severity, { | ||
| Map<String, Object?>? payload, | ||
| Map<String, String>? labels, | ||
| StackTrace? stackTrace, | ||
| }) { | ||
| final payloadStr = payload != null ? ' $payload' : ''; | ||
kevmoo marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| final labelsStr = labels != null && labels.isNotEmpty ? ' $labels' : ''; | ||
| final traceStr = stackTrace != null | ||
| ? '\n${formatStackTrace(stackTrace)}' | ||
| : ''; | ||
| if (severity == LogSeverity.defaultSeverity) { | ||
| print('$message$payloadStr$labelsStr$traceStr'); | ||
| } else { | ||
| print('${severity.name}: $message$payloadStr$labelsStr$traceStr'); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @internal | ||
| bool frameFolder(Frame frame) => | ||
| frame.isCore || frame.package == 'google_cloud'; | ||
|
|
||
| @internal | ||
| Chain formatStackTrace(StackTrace? stackTrace) => | ||
| (stackTrace == null ? Chain.current() : Chain.forTrace(stackTrace)) | ||
| .foldFrames(frameFolder, terse: true); | ||
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.
Uh oh!
There was an error while loading. Please reload this page.