LoggerFor returns the logger for the provided Context. Each call for a new context creates a per-scenario temp file for log isolation.
(ctx context.Context)
| 156 | // LoggerFor returns the logger for the provided Context. Each call for |
| 157 | // a new context creates a per-scenario temp file for log isolation. |
| 158 | func LoggerFor(ctx context.Context) (Logger, context.Context) { |
| 159 | if logger, ok := ctx.Value(loggerKey).(Logger); ok { |
| 160 | return logger, ctx |
| 161 | } |
| 162 | |
| 163 | id := counter.Add(1) |
| 164 | |
| 165 | f, err := os.CreateTemp("", fmt.Sprintf("scenario-%010d-*.log", id)) |
| 166 | if err != nil { |
| 167 | panic(fmt.Sprintf("failed to create scenario log file: %v", err)) |
| 168 | } |
| 169 | |
| 170 | delegate := &fileLogger{file: f} |
| 171 | |
| 172 | logger := logger{ |
| 173 | t: delegate, |
| 174 | id: id, |
| 175 | name: "*", |
| 176 | path: f.Name(), |
| 177 | } |
| 178 | |
| 179 | return &logger, context.WithValue(ctx, loggerKey, &logger) |
| 180 | } |