Files
icecast-ripper/internal/logger/logger.go
Dmitry Andreev b5f23a50b7
Some checks failed
CodeQL / Analyze (go) (push) Has been cancelled
Docker / build (push) Has been cancelled
golangci-lint / lint (push) Has been cancelled
Integrate MP3 duration extraction and silence handling (#6)
* feat: integrate MP3 duration extraction and silence handling

- Added a new dependency on github.com/tcolgate/mp3 for MP3 frame decoding.
- Implemented actual MP3 duration retrieval in the scanRecordings function, falling back to an estimation if retrieval fails.
- Introduced a silent frame asset for generating silence in audio streams.
- Created a silence reader to provide a continuous stream of silent frames.
- Added necessary documentation and licensing for the new MP3 package.
- Updated .gitignore to exclude MP3 files except for the internal data directory.

* feat: update README and improve application configuration and logging

* refactor: remove unused GenerateFileHash function and improve resource cleanup in MP3 and recorder modules
2025-04-13 15:56:22 +03:00

61 lines
1.2 KiB
Go

package logger
import (
"log/slog"
"os"
"strings"
)
// Format represents the output format for logs
type Format string
const (
// JSON outputs logs in JSON format for machine readability
JSON Format = "json"
// Text outputs logs in a human-readable format
Text Format = "text"
)
// Setup initializes the structured logger with the specified log level and format
func Setup(logLevel string, format ...Format) {
level := parseLogLevel(logLevel)
// Default to JSON format if not specified
logFormat := JSON
if len(format) > 0 {
logFormat = format[0]
}
var handler slog.Handler
switch logFormat {
case Text:
handler = slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
Level: level,
})
default: // JSON
handler = slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
Level: level,
})
}
logger := slog.New(handler)
slog.SetDefault(logger)
slog.Debug("Logger initialized", "level", level.String(), "format", string(logFormat))
}
// parseLogLevel converts a string log level to slog.Level
func parseLogLevel(level string) slog.Level {
switch strings.ToLower(level) {
case "debug":
return slog.LevelDebug
case "warn", "warning":
return slog.LevelWarn
case "error":
return slog.LevelError
default:
return slog.LevelInfo
}
}