Skip to content

⚡️ Use fine-grained logger instead of stdout.writeln #694

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 14 commits into from
Jul 16, 2025

Conversation

AlexV525
Copy link
Member

@AlexV525 AlexV525 commented Jul 11, 2025

What does this change?

Fixes #693

This PR also deletes the deprecation header. And added test configurations to let tests run with concurrency=1.

Type of change

  • Bug fix (non-breaking change which fixes an issue)

Copy link

codecov bot commented Jul 11, 2025

Codecov Report

Attention: Patch coverage is 84.21053% with 3 lines in your changes missing coverage. Please review.

Project coverage is 96.61%. Comparing base (675eb2e) to head (77654b6).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
packages/core/lib/settings/config.dart 70.00% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #694      +/-   ##
==========================================
- Coverage   96.62%   96.61%   -0.01%     
==========================================
  Files          22       23       +1     
  Lines         858      856       -2     
==========================================
- Hits          829      827       -2     
  Misses         29       29              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@AlexV525 AlexV525 marked this pull request as ready for review July 11, 2025 04:43
@davidmorgan
Copy link
Contributor

Thanks! I was able to try it by adding path deps from your example folder.

+  flutter_gen_core:
+    path: ../../packages/core
+  flutter_gen:
+    path: ../../packages/command
   flutter_test:
     sdk: flutter
   flutter_gen_runner:
@@ -25,6 +29,10 @@ dev_dependencies:
   lints: ^2.0.0
   build_runner: ^2.0.0
 
+dependency_overrides:
+  flutter_gen_core:
+    path: ../../packages/core
+

then running

dart run build_runner build -dv

here is the output

Compiling the build script.                                                                                                                                                                                                                                  
[FlutterGen] v5.10.0 Loading ...
[FlutterGen] Reading options from pubspec.yaml
0s flutter_gen_runner on 1 input: 1 output                                                                                                                                                                                                                   
                                                                                                                                                                                                                                                             
Built with build_runner in 2s with warnings; wrote 3 outputs.                                                                                                                                                                                                
                                                                                                                                                                                                                                                             
log output for flutter_gen_runner on $package$                                                                                                                                                                                                               
W [FlutterGen] Generated: lib/gen/assets.gen.dart                                                                                                                                                                                                            
W [FlutterGen] Generated: lib/gen/colors.gen.dart                                                                                                                                                                                                            
W [FlutterGen] Generated: lib/gen/fonts.gen.dart                                                                                                                                                                                                             
W [FlutterGen] Finished generating.                                                                                                                                                                                                                          
log output for build_runner                                                                                                                                                                                                                                  
  Deleting 3 declared outputs which already existed on disk.

I still see two problems:

  1. The [FlutterGen] v5.10.0 still appears as before. Digging a bit I think this is because it's printed during the first call to buildExtensions, which build_runner does not expect today :) ... I filed an issue for me to treat prints from buildExtensions the same as from build Capture prints during buildExtensions dart-lang/build#4092

  2. I had forgotten that print shows up as a warning, which means the build ends up saying "with warnings" and the output lines show with a W. This is probably not what we want, it should be info level. To get that it needs to call log.info instead of print; but it can't because the code calling print does not know build_runner.

Fortunately, there is a way to do it :) like this, in flutter_gen_runner.dart:

    await runZoned(
      () => generator.build(
        config: _config,
        writer: (contents, path) {
          buildStep.writeAsString(_output(buildStep, path), contents);
        },
      ),
      zoneSpecification: ZoneSpecification(
        print: (_, __, ___, message) {
          log.info(message);
        },
      ),
    );

now prints are logged as info. You could also write more code here if you want to separate some messages into info and some into warnings/errors.

With the -v flag making infos visible the output is now

[FlutterGen] v5.10.0 Loading ...
[FlutterGen] Reading options from pubspec.yaml
1s flutter_gen_runner on 1 input: 1 output                                                                                                                                                                                                                   
                                                                                                                                                                                                                                                             
Built with build_runner in 2s; wrote 3 outputs.                                                                                                                                                                                                              
                                                                                                                                                                                                                                                             
log output for flutter_gen_runner on $package$                                                                                                                                                                                                               
  [FlutterGen] Generated: lib/gen/assets.gen.dart                                                                                                                                                                                                            
  [FlutterGen] Generated: lib/gen/colors.gen.dart                                                                                                                                                                                                            
  [FlutterGen] Generated: lib/gen/fonts.gen.dart                                                                                                                                                                                                             
  [FlutterGen] Finished generating.

if you don't pass -v then infos are not visible by default, so the output is

Generating the build script.                                                                                                                                                                                                                                 
[FlutterGen] v5.10.0 Loading ...
[FlutterGen] Reading options from pubspec.yaml
0s flutter_gen_runner on 1 input: 1 skipped                                                                                                                                                                                                                  
                                                                                                                                                                                                                                                             
Built with build_runner in 1s; wrote 0 outputs.

After I make the fix to build_runner to capture from the extension getter, we should end up with two versions, without -v:

Generating the build script.                                                                                                                                                                                                                                 
0s flutter_gen_runner on 1 input: 1 skipped                                                                                                                                                                                                                  
                                                                                                                                                                                                                                                             
Built with build_runner in 1s; wrote 0 outputs.

and with -v

1s flutter_gen_runner on 1 input: 1 output                                                                                                                                                                                                                   
                                                                                                                                                                                                                                                             
Built with build_runner in 2s; wrote 3 outputs.                                                                                                                                                                                                              
                                                                                                                                                                                                                                                             
log output for flutter_gen_runner on $package$                                                                                                                                                                                                               
  [FlutterGen] v5.10.0 Loading ...
  [FlutterGen] Reading options from pubspec.yaml
  [FlutterGen] Generated: lib/gen/assets.gen.dart                                                                                                                                                                                                            
  [FlutterGen] Generated: lib/gen/colors.gen.dart                                                                                                                                                                                                            
  [FlutterGen] Generated: lib/gen/fonts.gen.dart                                                                                                                                                                                                             
  [FlutterGen] Finished generating.

which I think looks good, what do you think please? :)

Thanks.

@AlexV525
Copy link
Member Author

@davidmorgan I've switched to the package:logging, the executable will redirect the log records to stdout / stderr based on the log level.

@AlexV525 AlexV525 changed the title ⚡️ Use print instead of stdout.writeln ⚡️ Use fine-grained logger instead of stdout.writeln Jul 12, 2025
@davidmorgan
Copy link
Contributor

Thanks! That's a good direction, one more suggestion, here:

void main(List<String> args) async {
  log.onRecord.listen((record) {
    if (record.level >= Level.SEVERE) {
      stderr.writeln(record.message);
    } else {
      stdout.writeln(record.message);
    }
  });

I suggest moving the [FlutterGen] and [WARNING] prefixes out of the messages, to here, something like:

void main(List<String> args) async {
  log.onRecord.listen((record) {
    if (record.level >= Level.SEVERE) {
      stderr.writeln('[FlutterGen] [WARNING] ${record.message});
    } else {
      stdout.writeln('[FlutterGen] ${record.message}');
    }
  });

This is because build_runner will add the generator name + severity to its output, so this way you avoid getting them twice.

Also I notice that for log.severe you sometimes pass an exception, build_runner will display it, if you want that printed when running without build_runner I think you need something like

void main(List<String> args) async {
  log.onRecord.listen((record) {
    if (record.level >= Level.SEVERE) {
      var message = record.message;
      if (record.error != null) message += '\n${record.error}';
      if (record.stackTrace != null) message += '\n${record.stackTrace}';
      stderr.writeln('[FlutterGen] [WARNING] $message);
    } else {
      stdout.writeln('[FlutterGen] ${record.message}');
    }
  });

Thanks :)

@davidmorgan
Copy link
Contributor

Looks good to me--thanks :)

@AlexV525
Copy link
Member Author

Looks good to me--thanks :)

Thanks for keeping an eye on this!

@AlexV525 AlexV525 merged commit 12ea28d into main Jul 16, 2025
4 of 5 checks passed
@AlexV525 AlexV525 deleted the feat/get-rid-of-stdout branch July 16, 2025 15:39
@AlexV525 AlexV525 mentioned this pull request Jul 17, 2025
AlexV525 added a commit that referenced this pull request Jul 17, 2025
## 5.11.0

**Feature**

- [#576](#576) Add support
for deferred components. by [@ianmaciel](https://github.com/ianmaciel)
- [#676](#676) Add new
option `parse_animation` to parse metadata for animated images. by
[@huandu](https://github.com/huandu)
- [#680](#680) Add svg
`ColorMapper` to svg loader. by [@AlexV525](https://github.com/AlexV525)
- [#685](#685) Use `.vec`
SVG class for `vector_graphics_compiler` transformed assets. by
[@Albert221](https://github.com/Albert221)
- [#697](#697) Refactor
how generated files are being formatted. by
[@AlexV525](https://github.com/AlexV525)

**Development**

- [#681](#682) Bump
`dart_style` v3 which also requires Dart 3.4. by
[@AlexV525](https://github.com/AlexV525)
- [#682](#682) Add Facts
generate utils. by [@AlexV525](https://github.com/AlexV525)
- [#694](#694) Use
fine-grained logger instead of `stdout.writeln`. by
[@AlexV525](https://github.com/AlexV525)
- [#698](#698) Improve
workflow with automatic formatting. by
[@AlexV525](https://github.com/AlexV525)
- [#699](#699) Allow
`build 3.0.0`. by [@davidmorgan](https://github.com/davidmorgan)
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.

[BUG]: stdout.write breaks build_runner output
2 participants