Skip to content

Add Manager.SetLogger and WithDeferredLogger: the logger configured by the config itself - #52

Merged
Insei merged 4 commits into
mainfrom
deferred-logger
Oct 18, 2026
Merged

Insei merged 4 commits into
mainfrom
deferred-logger

Conversation

@Insei

@Insei Insei commented Oct 18, 2026

Copy link
Copy Markdown
Owner

The logger is often configured by the config, but the config is parsed by the manager that already needs the logger. WithDeferredLogger keeps the log records in memory, and Manager.SetLogger writes them to the logger created from the parsed config.

config, _ := tinyconf.New(tinyconf.WithDriver(...), tinyconf.WithDeferredLogger())
err := config.Parse(ctx, conf)
config.SetLogger(newLogger(conf.Logging)) // the records of Parse are written to it
tinyconf.Subscribe(ctx, config, func(_, next *Logging) { config.SetLogger(newLogger(*next)) })
  • SetLogger is safe for concurrent use: the logger is stored in atomic.Pointer, the kept records are written under the mutex, the records written concurrently wait for them, and the loggers taken before SetLogger (by Parse running at the same time) write to the current logger of the manager. Nothing is lost, the order is kept.
  • Up to 1000 records are kept, the oldest are dropped with a warning about their count. SetLogger(nil) disables logging.
  • Without WithDeferredLogger nothing is kept, SetLogger only replaces the logger; the behaviour of the existing code is not changed.
  • ExampleWithDeferredLogger with slog (Go 1.21+), the example service creates its logger from the config and follows the changes of the level, checked by hand with the change of config.yaml.
  • Race test of SetLogger with concurrent Parse and writers.

Insei added 4 commits October 19, 2026 02:24
…red by the config itself

Signed-off-by: Dmitriy Aleksandrov <goodmobiledevices@gmail.com>
Signed-off-by: Dmitriy Aleksandrov <goodmobiledevices@gmail.com>
…Logger

Signed-off-by: Dmitriy Aleksandrov <goodmobiledevices@gmail.com>
…its level

Signed-off-by: Dmitriy Aleksandrov <goodmobiledevices@gmail.com>
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (335bb93) to head (6e74f4b).

Additional details and impacted files
@@            Coverage Diff            @@
##              main       #52   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           31        32    +1     
  Lines         2581      2630   +49     
=========================================
+ Hits          2581      2630   +49     

☔ View full report in Codecov by Harness.
📢 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.

@github-actions

Copy link
Copy Markdown
Contributor

Benchmark diff: 335bb93 → 5680f8c (10 runs)

goos: linux
goarch: amd64
pkg: github.com/insei/tinyconf/v2
cpu: AMD EPYC 7763 64-Core Processor                
                    │  base.txt   │              head.txt              │
                    │   sec/op    │   sec/op     vs base               │
Parse-4               8.516µ ± 1%   8.518µ ± 1%       ~ (p=0.796 n=10)
Parse_SubConfig-4     3.008µ ± 0%   3.050µ ± 2%  +1.40% (p=0.000 n=10)
Current-4             55.16n ± 0%   55.33n ± 0%  +0.30% (p=0.024 n=10)
Current_SubConfig-4   98.02n ± 0%   98.81n ± 1%  +0.82% (p=0.000 n=10)
geomean               610.0n        613.9n       +0.64%

                    │    base.txt    │               head.txt                │
                    │      B/op      │     B/op      vs base                 │
Parse-4               4.899Ki ± 0%     4.899Ki ± 0%       ~ (p=1.000 n=10) ¹
Parse_SubConfig-4     1.367Ki ± 0%     1.367Ki ± 0%       ~ (p=1.000 n=10) ¹
Current-4               0.000 ± 0%       0.000 ± 0%       ~ (p=1.000 n=10) ¹
Current_SubConfig-4     0.000 ± 0%       0.000 ± 0%       ~ (p=1.000 n=10) ¹
geomean                            ²                 +0.00%                ²
¹ all samples are equal
² summaries must be >0 to compute geomean

                    │   base.txt   │              head.txt               │
                    │  allocs/op   │ allocs/op   vs base                 │
Parse-4               139.0 ± 0%     139.0 ± 0%       ~ (p=1.000 n=10) ¹
Parse_SubConfig-4     44.00 ± 0%     44.00 ± 0%       ~ (p=1.000 n=10) ¹
Current-4             0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10) ¹
Current_SubConfig-4   0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10) ¹
geomean                          ²               +0.00%                ²
¹ all samples are equal
² summaries must be >0 to compute geomean

pkg: github.com/insei/tinyconf/v2/drivers
                │  base.txt   │           head.txt            │
                │   sec/op    │   sec/op     vs base          │
Parse_Drivers-4   75.05µ ± 1%   75.22µ ± 2%  ~ (p=0.739 n=10)

                │   base.txt   │            head.txt            │
                │     B/op     │     B/op      vs base          │
Parse_Drivers-4   32.04Ki ± 0%   32.04Ki ± 0%  ~ (p=1.000 n=10)

                │  base.txt  │            head.txt            │
                │ allocs/op  │ allocs/op   vs base            │
Parse_Drivers-4   645.0 ± 0%   645.0 ± 0%  ~ (p=1.000 n=10) ¹
¹ all samples are equal

@Insei
Insei merged commit 7e2b443 into main Oct 18, 2026
4 checks passed
@Insei
Insei deleted the deferred-logger branch October 18, 2026 23:27
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.

2 participants