Skip to content

Commit 8151b9f

Browse files
authored
Merge pull request #401 from dart-lang/merge-logging-package
Merge `package:logging`
2 parents 63e934c + 31caf59 commit 8151b9f

17 files changed

+1670
-0
lines changed

.github/ISSUE_TEMPLATE/logging.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
name: "package:logging"
3+
about: "Create a bug or file a feature request against package:logging."
4+
labels: "package:logging"
5+
---

.github/labeler.yml

+4
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,7 @@
2323
"package:fixnum":
2424
- changed-files:
2525
- any-glob-to-any-file: 'pkgs/fixnum/**'
26+
27+
"package:logging":
28+
- changed-files:
29+
- any-glob-to-any-file: 'pkgs/logging/**'

.github/workflows/logging.yaml

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
name: package:logging
2+
3+
on:
4+
# Run CI on pushes to the main branch, and on PRs against main.
5+
push:
6+
branches: [ main ]
7+
paths:
8+
- '.github/workflows/logging.yaml'
9+
- 'pkgs/logging/**'
10+
pull_request:
11+
branches: [ main ]
12+
paths:
13+
- '.github/workflows/logging.yaml'
14+
- 'pkgs/logging/**'
15+
schedule:
16+
- cron: "0 0 * * 0"
17+
env:
18+
PUB_ENVIRONMENT: bot.github
19+
20+
defaults:
21+
run:
22+
working-directory: pkgs/logging/
23+
24+
jobs:
25+
# Check code formatting and static analysis.
26+
analyze:
27+
runs-on: ubuntu-latest
28+
strategy:
29+
fail-fast: false
30+
matrix:
31+
sdk: [dev]
32+
steps:
33+
- uses: actions/checkout@d632683dd7b4114ad314bca15554477dd762a938
34+
- uses: dart-lang/setup-dart@0a8a0fc875eb934c15d08629302413c671d3f672
35+
with:
36+
sdk: ${{ matrix.sdk }}
37+
- id: install
38+
name: Install dependencies
39+
run: dart pub get
40+
- name: Check formatting
41+
run: dart format --output=none --set-exit-if-changed .
42+
if: always() && steps.install.outcome == 'success'
43+
- name: Analyze code
44+
run: dart analyze --fatal-infos
45+
if: always() && steps.install.outcome == 'success'
46+
47+
# Run tests on a matrix of platforms and sdk versions.
48+
test:
49+
needs: analyze
50+
runs-on: ${{ matrix.os }}
51+
strategy:
52+
fail-fast: false
53+
matrix:
54+
# Add macos-latest and/or windows-latest if relevant for this package.
55+
os: [ubuntu-latest]
56+
sdk: [3.4, dev]
57+
steps:
58+
- uses: actions/checkout@d632683dd7b4114ad314bca15554477dd762a938
59+
- uses: dart-lang/setup-dart@0a8a0fc875eb934c15d08629302413c671d3f672
60+
with:
61+
sdk: ${{ matrix.sdk }}
62+
- id: install
63+
name: Install dependencies
64+
run: dart pub get
65+
- name: Run VM tests
66+
run: dart test --platform vm
67+
if: always() && steps.install.outcome == 'success'
68+
- name: Run Chrome tests
69+
run: dart test --platform chrome
70+
if: always() && steps.install.outcome == 'success'

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ This repository is home to various Dart packages under the [dart.dev](https://pu
1414
| [convert](pkgs/convert/) | Utilities for converting between data representations. | [![pub package](https://img.shields.io/pub/v/convert.svg)](https://pub.dev/packages/convert) |
1515
| [crypto](pkgs/crypto/) | Implementations of SHA, MD5, and HMAC cryptographic functions. | [![pub package](https://img.shields.io/pub/v/crypto.svg)](https://pub.dev/packages/crypto) |
1616
| [fixnum](pkgs/fixnum/) | Library for 32- and 64-bit signed fixed-width integers. | [![pub package](https://img.shields.io/pub/v/fixnum.svg)](https://pub.dev/packages/fixnum) |
17+
| [logging](pkgs/logging/) | Provides APIs for debugging and error logging. | [![pub package](https://img.shields.io/pub/v/logging.svg)](https://pub.dev/packages/logging) |
1718

1819
## Publishing automation
1920

pkgs/logging/.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.dart_tool
2+
.packages
3+
pubspec.lock

pkgs/logging/AUTHORS

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Names should be added to this file with this pattern:
2+
#
3+
# For individuals:
4+
# Name <email address>
5+
#
6+
# For organizations:
7+
# Organization <fnmatch pattern>
8+
#
9+
Google Inc. <*@google.com>
10+
Anton Astashov <[email protected]>

pkgs/logging/CHANGELOG.md

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
## 1.3.0
2+
3+
* Override empty stack traces for trace level events.
4+
* Require Dart 3.4
5+
* Move to `dart-lang/core` monorepo.
6+
7+
## 1.2.0
8+
9+
* Add notification when the log level is changed. Logger `onLevelChanged` broadcasts a stream of level values.
10+
* Require Dart 2.19.
11+
12+
## 1.1.1
13+
14+
* Add a check that throws if a logger name ends with '.'.
15+
* Require Dart 2.18
16+
17+
## 1.1.0
18+
19+
* Add `Logger.attachedLoggers` which exposes all loggers created with the
20+
default constructor.
21+
* Enable the `avoid_dynamic_calls` lint.
22+
23+
## 1.0.2
24+
25+
* Update description.
26+
* Add example.
27+
28+
## 1.0.1
29+
30+
* List log levels in README.
31+
32+
## 1.0.0
33+
34+
* Stable null safety release.
35+
36+
## 1.0.0-nullsafety.0
37+
38+
* Migrate to null safety.
39+
* Removed the deprecated `LoggerHandler` typedef.
40+
41+
## 0.11.4
42+
43+
* Add top level `defaultLevel`.
44+
* Require Dart `>=2.0.0`.
45+
* Make detached loggers work regardless of `hierarchicalLoggingEnabled`.
46+
47+
## 0.11.3+2
48+
49+
* Set max SDK version to `<3.0.0`, and adjust other dependencies.
50+
51+
## 0.11.3+1
52+
53+
* Fixed several documentation comments.
54+
55+
## 0.11.3
56+
57+
* Added optional `LogRecord.object` field.
58+
59+
* `Logger.log` sets `LogRecord.object` if the message is not a string or a
60+
function that returns a string. So that a handler can access the original
61+
object instead of just its `toString()`.
62+
63+
## 0.11.2
64+
65+
* Added `Logger.detached` - a convenience factory to obtain a logger that is not
66+
attached to this library's logger hierarchy.
67+
68+
## 0.11.1+1
69+
70+
* Include default error with the auto-generated stack traces.
71+
72+
## 0.11.1
73+
74+
* Add support for automatically logging the stack trace on error messages. Note
75+
this can be expensive, so it is off by default.
76+
77+
## 0.11.0
78+
79+
* Revert change in `0.10.0`. `stackTrace` must be an instance of `StackTrace`.
80+
Use the `Trace` class from the [stack_trace package][] to convert strings.
81+
82+
[stack_trace package]: https://pub.dev/packages/stack_trace
83+
84+
## 0.10.0
85+
86+
* Change type of `stackTrace` from `StackTrace` to `Object`.
87+
88+
## 0.9.3
89+
90+
* Added optional `LogRecord.zone` field.
91+
92+
* Record current zone (or user specified zone) when creating new `LogRecord`s.

pkgs/logging/LICENSE

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Copyright 2013, the Dart project authors.
2+
3+
Redistribution and use in source and binary forms, with or without
4+
modification, are permitted provided that the following conditions are
5+
met:
6+
7+
* Redistributions of source code must retain the above copyright
8+
notice, this list of conditions and the following disclaimer.
9+
* Redistributions in binary form must reproduce the above
10+
copyright notice, this list of conditions and the following
11+
disclaimer in the documentation and/or other materials provided
12+
with the distribution.
13+
* Neither the name of Google LLC nor the names of its
14+
contributors may be used to endorse or promote products derived
15+
from this software without specific prior written permission.
16+
17+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21+
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

pkgs/logging/README.md

+141
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
[![Dart CI](https://github.com/dart-lang/core/actions/workflows/logging.yaml/badge.svg)](https://github.com/dart-lang/core/actions/workflows/logging.yaml)
2+
[![Pub](https://img.shields.io/pub/v/logging.svg)](https://pub.dev/packages/logging)
3+
[![package publisher](https://img.shields.io/pub/publisher/logging.svg)](https://pub.dev/packages/logging/publisher)
4+
5+
## Initializing
6+
7+
By default, the logging package does not do anything useful with the log
8+
messages. You must configure the logging level and add a handler for the log
9+
messages.
10+
11+
Here is a simple logging configuration that logs all messages via `print`.
12+
13+
```dart
14+
Logger.root.level = Level.ALL; // defaults to Level.INFO
15+
Logger.root.onRecord.listen((record) {
16+
print('${record.level.name}: ${record.time}: ${record.message}');
17+
});
18+
```
19+
20+
First, set the root `Level`. All messages at or above the current level are sent to the
21+
`onRecord` stream. Available levels are:
22+
23+
+ `Level.OFF`
24+
+ `Level.SHOUT`
25+
+ `Level.SEVERE`
26+
+ `Level.WARNING`
27+
+ `Level.INFO`
28+
+ `Level.CONFIG`
29+
+ `Level.FINE`
30+
+ `Level.FINER`
31+
+ `Level.FINEST`
32+
33+
Then, listen on the `onRecord` stream for `LogRecord` events. The `LogRecord`
34+
class has various properties for the message, error, logger name, and more.
35+
36+
To listen for changed level notifications use:
37+
38+
```dart
39+
Logger.root.onLevelChanged.listen((level) {
40+
print('The new log level is $level');
41+
});
42+
```
43+
44+
## Logging messages
45+
46+
Create a `Logger` with a unique name to easily identify the source of the log
47+
messages.
48+
49+
```dart
50+
final log = Logger('MyClassName');
51+
```
52+
53+
Here is an example of logging a debug message and an error:
54+
55+
```dart
56+
var future = doSomethingAsync().then((result) {
57+
log.fine('Got the result: $result');
58+
processResult(result);
59+
}).catchError((e, stackTrace) => log.severe('Oh noes!', e, stackTrace));
60+
```
61+
62+
When logging more complex messages, you can pass a closure instead that will be
63+
evaluated only if the message is actually logged:
64+
65+
```dart
66+
log.fine(() => [1, 2, 3, 4, 5].map((e) => e * 4).join("-"));
67+
```
68+
69+
Available logging methods are:
70+
71+
+ `log.shout(logged_content);`
72+
+ `log.severe(logged_content);`
73+
+ `log.warning(logged_content);`
74+
+ `log.info(logged_content);`
75+
+ `log.config(logged_content);`
76+
+ `log.fine(logged_content);`
77+
+ `log.finer(logged_content);`
78+
+ `log.finest(logged_content);`
79+
80+
## Configuration
81+
82+
Loggers can be individually configured and listened to. When an individual logger has no
83+
specific configuration, it uses the configuration and any listeners found at `Logger.root`.
84+
85+
To begin, set the global boolean `hierarchicalLoggingEnabled` to `true`.
86+
87+
Then, create unique loggers and configure their `level` attributes and assign any listeners to
88+
their `onRecord` streams.
89+
90+
91+
```dart
92+
hierarchicalLoggingEnabled = true;
93+
Logger.root.level = Level.WARNING;
94+
Logger.root.onRecord.listen((record) {
95+
print('[ROOT][WARNING+] ${record.message}');
96+
});
97+
98+
final log1 = Logger('FINE+');
99+
log1.level = Level.FINE;
100+
log1.onRecord.listen((record) {
101+
print('[LOG1][FINE+] ${record.message}');
102+
});
103+
104+
// log2 inherits LEVEL value of WARNING from `Logger.root`
105+
final log2 = Logger('WARNING+');
106+
log2.onRecord.listen((record) {
107+
print('[LOG2][WARNING+] ${record.message}');
108+
});
109+
110+
111+
// Will NOT print because FINER is too low level for `Logger.root`.
112+
log1.finer('LOG_01 FINER (X)');
113+
114+
// Will print twice ([LOG1] & [ROOT])
115+
log1.fine('LOG_01 FINE (√√)');
116+
117+
// Will print ONCE because `log1` only uses root listener.
118+
log1.warning('LOG_01 WARNING (√)');
119+
120+
// Will never print because FINE is too low level.
121+
log2.fine('LOG_02 FINE (X)');
122+
123+
// Will print twice ([LOG2] & [ROOT]) because warning is sufficient for all
124+
// loggers' levels.
125+
log2.warning('LOG_02 WARNING (√√)');
126+
127+
// Will never print because `info` is filtered by `Logger.root.level` of
128+
// `Level.WARNING`.
129+
log2.info('INFO (X)');
130+
```
131+
132+
Results in:
133+
134+
```
135+
[LOG1][FINE+] LOG_01 FINE (√√)
136+
[ROOT][WARNING+] LOG_01 FINE (√√)
137+
[LOG1][FINE+] LOG_01 WARNING (√)
138+
[ROOT][WARNING+] LOG_01 WARNING (√)
139+
[LOG2][WARNING+] LOG_02 WARNING (√√)
140+
[ROOT][WARNING+] LOG_02 WARNING (√√)
141+
```

0 commit comments

Comments
 (0)