()
| 1408 | } |
| 1409 | |
| 1410 | func (app *BaseApp) initLogger() error { |
| 1411 | duration := 3 * time.Second |
| 1412 | ticker := time.NewTicker(duration) |
| 1413 | done := make(chan bool, 1) |
| 1414 | |
| 1415 | handler := logger.NewBatchHandler(logger.BatchOptions{ |
| 1416 | Level: getLoggerMinLevel(app), |
| 1417 | BatchSize: 200, |
| 1418 | BeforeAddFunc: func(ctx context.Context, log *logger.Log) bool { |
| 1419 | if app.IsDev() { |
| 1420 | printLog(log) |
| 1421 | |
| 1422 | // manually check the log level and skip if necessary |
| 1423 | if log.Level < slog.Level(app.Settings().Logs.MinLevel) { |
| 1424 | return false |
| 1425 | } |
| 1426 | } |
| 1427 | |
| 1428 | ticker.Reset(duration) |
| 1429 | |
| 1430 | return app.Settings().Logs.MaxDays > 0 |
| 1431 | }, |
| 1432 | WriteFunc: func(ctx context.Context, logs []*logger.Log) error { |
| 1433 | if !app.IsBootstrapped() || app.Settings().Logs.MaxDays == 0 { |
| 1434 | return nil |
| 1435 | } |
| 1436 | |
| 1437 | // write the accumulated logs |
| 1438 | // (note: based on several local tests there is no significant performance difference between small number of separate write queries vs 1 big INSERT) |
| 1439 | app.AuxRunInTransaction(func(txApp App) error { |
| 1440 | model := &Log{} |
| 1441 | for _, l := range logs { |
| 1442 | model.MarkAsNew() |
| 1443 | model.Id = GenerateDefaultRandomId() |
| 1444 | model.Level = int(l.Level) |
| 1445 | model.Message = l.Message |
| 1446 | model.Data = l.Data |
| 1447 | model.Created, _ = types.ParseDateTime(l.Time) |
| 1448 | |
| 1449 | if err := txApp.AuxSave(model); err != nil { |
| 1450 | log.Println("Failed to write log", model, err) |
| 1451 | } |
| 1452 | } |
| 1453 | |
| 1454 | return nil |
| 1455 | }) |
| 1456 | |
| 1457 | return nil |
| 1458 | }, |
| 1459 | }) |
| 1460 | |
| 1461 | go func() { |
| 1462 | ctx := context.Background() |
| 1463 | |
| 1464 | for { |
| 1465 | select { |
| 1466 | case <-done: |
| 1467 | return |
no test coverage detected