Skip to content

Commit f6a68a3

Browse files
feat(backend, logger): new setting on TOML that allows to specify a new path for the logger
1 parent cfdcdcd commit f6a68a3

6 files changed

Lines changed: 26 additions & 6 deletions

File tree

backend/cmd/config.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"github.com/HyperloopUPV-H8/h9-backend/internal/server"
55
"github.com/HyperloopUPV-H8/h9-backend/internal/vehicle"
6+
"github.com/HyperloopUPV-H8/h9-backend/pkg/logger"
67
)
78

89
type App struct {
@@ -46,7 +47,8 @@ type TCP struct {
4647
}
4748

4849
type Logging struct {
49-
TimeUnit string `toml:"time_unit"`
50+
TimeUnit logger.TimeUnit `toml:"time_unit"`
51+
LoggingPath string `toml:"logging_path"`
5052
}
5153

5254
type Config struct {

backend/cmd/config.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ backoff_factor = 2 # Backoff multiplier for retry delays
5353
enable_progress = true # Enable progress callbacks during transfers
5454

5555
# Logger Configuration
56+
[logging]
5657
time_unit = "us" # Time unit for log timestamps (ns, us, ms, s) Default: ns
58+
logging_path = "." # Absolute path of the logger
5759

5860
# <-- DO NOT TOUCH BELOW THIS LINE -->
5961

backend/cmd/dev-config.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,5 @@ file = "backend.log" # Log file path (empty to disable file logging)
8484
max_size_mb = 100 # Maximum log file size in MB
8585
max_backups = 3 # Number of backup files to keep
8686
max_age_days = 7 # Maximum age of log files in days
87-
time_unit = "us" # Time unit for log timestamps (ns, us, ms, s)
87+
time_unit = "us" # Time unit for log timestamps (ns, us, ms, s)
88+
logging_path = "."

backend/cmd/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ func main() {
176176
order_logger.Name: order_logger.NewLogger(),
177177
}
178178

179-
logger.SetFormatTimestamp(logger.TimeUnit(config.Logging.TimeUnit)) // MUST be before creating subloggers
179+
logger.ConfigureLogger(config.Logging.TimeUnit, config.Logging.LoggingPath)
180180
loggerHandler := logger.NewLogger(subloggers, trace.Logger)
181181

182182
// <--- order transfer --->

backend/pkg/logger/base/logger.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,19 @@ func (sublogger *BaseLogger) Start() error {
5656

5757
func (sublogger *BaseLogger) CreateFile(filename string) (*os.File, error) {
5858

59-
err := os.MkdirAll(path.Dir(filename), os.ModePerm)
59+
// Includes the direcory specified by the user
60+
baseFilename := path.Join(loggerHandler.BasePath, filename)
61+
62+
err := os.MkdirAll(path.Dir(baseFilename), os.ModePerm)
6063
if err != nil {
6164
return nil, loggerHandler.ErrCreatingAllDir{
6265
Name: sublogger.Name,
6366
Timestamp: time.Now(),
64-
Path: filename,
67+
Path: baseFilename,
6568
}
6669
}
6770

68-
return os.Create(path.Join(filename))
71+
return os.Create(path.Join(baseFilename))
6972
}
7073

7174
// Create a base Logger with default values

backend/pkg/logger/logger.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ var _ abstraction.Logger = &Logger{}
3434
// Used on subloggers to get the current timestamp for folder or file names
3535
var Timestamp = time.Now()
3636

37+
var BasePath = "."
38+
3739
func (Logger) HandlerName() string { return HandlerName }
3840

3941
func NewLogger(keys map[abstraction.LoggerName]abstraction.Logger, baseLogger zerolog.Logger) *Logger {
@@ -154,3 +156,13 @@ func (logger *Logger) Stop() error {
154156
logger.trace.Info().Msg("stopped")
155157
return nil
156158
}
159+
160+
// Configures the logger atributes before inicialicing it
161+
func ConfigureLogger(unit TimeUnit, basePath string) {
162+
163+
// Start the sublogger
164+
SetFormatTimestamp(unit)
165+
166+
// Update base Path
167+
BasePath = basePath
168+
}

0 commit comments

Comments
 (0)