timetable-extension #1
@ -26,7 +26,7 @@ func InitTicker(cfg *helpers.Configuration, stop <-chan struct{}) {
|
|||||||
|
|
||||||
// Runs the ticker and handles tick events
|
// Runs the ticker and handles tick events
|
||||||
func runTicker(cfg *helpers.Configuration, stop <-chan struct{}) {
|
func runTicker(cfg *helpers.Configuration, stop <-chan struct{}) {
|
||||||
log.Info("Starting background ticker", zap.Duration("frequency", frequency))
|
log.Info("Starting background task ticker", zap.Duration("frequency", frequency))
|
||||||
ticker := time.NewTicker(frequency)
|
ticker := time.NewTicker(frequency)
|
||||||
defer ticker.Stop()
|
defer ticker.Stop()
|
||||||
|
|
||||||
|
@ -36,7 +36,7 @@ func InitDataAccess(cfg *helpers.Configuration) {
|
|||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
log.Fatal("Error connecting to database: " + err.Error())
|
log.Fatal("Error connecting to database: " + err.Error())
|
||||||
} else {
|
} else {
|
||||||
log.Info("Database connection successful")
|
log.Info("Database connection initialising")
|
||||||
}
|
}
|
||||||
MongoClient = client
|
MongoClient = client
|
||||||
}
|
}
|
||||||
|
27
log/log.go
27
log/log.go
@ -7,27 +7,28 @@ import (
|
|||||||
"go.uber.org/zap/zapcore"
|
"go.uber.org/zap/zapcore"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Logger object is exported to facilitate migration
|
// Use outside `package log`
|
||||||
// to the single logger instance. Use outside `package log`
|
|
||||||
// should be avoided.
|
// should be avoided.
|
||||||
var post *zap.Logger
|
var post *zap.Logger
|
||||||
|
|
||||||
// Initialises the logger
|
// Initialises the logger
|
||||||
func init() {
|
func InitLogger() {
|
||||||
var err error
|
var err error
|
||||||
mode := os.Getenv("runtime")
|
mode := os.Getenv("runtime")
|
||||||
if mode == "" {
|
if mode == "" {
|
||||||
mode = "prod"
|
mode = "prod"
|
||||||
}
|
}
|
||||||
|
|
||||||
logLevel := zapcore.InfoLevel
|
var level zapcore.Level
|
||||||
if mode == "debug" {
|
if mode == "debug" {
|
||||||
logLevel = zapcore.DebugLevel
|
level = zap.DebugLevel
|
||||||
|
} else {
|
||||||
|
level = zap.InfoLevel
|
||||||
}
|
}
|
||||||
|
|
||||||
config := zap.NewDevelopmentConfig()
|
config := zap.NewDevelopmentConfig()
|
||||||
config.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
|
config.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
|
||||||
config.Level = zap.NewAtomicLevelAt(logLevel)
|
config.Level = zap.NewAtomicLevelAt(level)
|
||||||
post, err = config.Build(zap.AddCallerSkip(1))
|
post, err = config.Build(zap.AddCallerSkip(1))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic("Failed to initialize logger: " + err.Error())
|
panic("Failed to initialize logger: " + err.Error())
|
||||||
@ -35,29 +36,41 @@ func init() {
|
|||||||
|
|
||||||
defer post.Sync()
|
defer post.Sync()
|
||||||
|
|
||||||
Info("Logger initialised", zap.String("level", logLevel.String()), zap.String("runtime", mode))
|
Info("Logger initialised", zap.String("level", level.String()), zap.String("runtime", mode))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Logs message at info level
|
||||||
func Info(msg string, fields ...zap.Field) {
|
func Info(msg string, fields ...zap.Field) {
|
||||||
post.Info(msg, fields...)
|
post.Info(msg, fields...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Logs message at debug level
|
||||||
func Debug(msg string, fields ...zap.Field) {
|
func Debug(msg string, fields ...zap.Field) {
|
||||||
post.Debug(msg, fields...)
|
post.Debug(msg, fields...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Logs message at warn level
|
||||||
func Warn(msg string, fields ...zap.Field) {
|
func Warn(msg string, fields ...zap.Field) {
|
||||||
post.Warn(msg, fields...)
|
post.Warn(msg, fields...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Logs message at error level
|
||||||
func Error(msg string, fields ...zap.Field) {
|
func Error(msg string, fields ...zap.Field) {
|
||||||
post.Error(msg, fields...)
|
post.Error(msg, fields...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Logs message at fatal level then call os.exit(1)
|
||||||
func Fatal(msg string, fields ...zap.Field) {
|
func Fatal(msg string, fields ...zap.Field) {
|
||||||
post.Fatal(msg, fields...)
|
post.Fatal(msg, fields...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Logs message at panic level the panics
|
||||||
func Panic(msg string, fields ...zap.Field) {
|
func Panic(msg string, fields ...zap.Field) {
|
||||||
post.Panic(msg, fields...)
|
post.Panic(msg, fields...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Flushes log messages
|
||||||
|
func Cleanup() {
|
||||||
|
Info("Flushing log messages")
|
||||||
|
post.Sync()
|
||||||
|
}
|
||||||
|
29
main.go
29
main.go
@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"syscall"
|
"syscall"
|
||||||
@ -18,7 +19,14 @@ import (
|
|||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
printStartupBanner()
|
||||||
|
fmt.Println("Version", helpers.Version, "\n\n")
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
log.InitLogger()
|
||||||
|
defer log.Cleanup()
|
||||||
log.Info("Initialising OwlBoard timetable-mgr", zap.String("version", helpers.Version))
|
log.Info("Initialising OwlBoard timetable-mgr", zap.String("version", helpers.Version))
|
||||||
cfg, err := helpers.LoadConfig()
|
cfg, err := helpers.LoadConfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -82,5 +90,26 @@ func cleanup(cfg *helpers.Configuration, stop chan<- struct{}) {
|
|||||||
|
|
||||||
time.Sleep(500 * time.Millisecond)
|
time.Sleep(500 * time.Millisecond)
|
||||||
|
|
||||||
|
log.Cleanup()
|
||||||
|
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func printStartupBanner() {
|
||||||
|
art := `
|
||||||
|
___ _ ____ _
|
||||||
|
/ _ \__ _| | __ ) ___ __ _ _ __ __| |
|
||||||
|
| | | \ \ /\ / / | _ \ / _ \ / _' | '__/ _' |
|
||||||
|
| |_| |\ V V /| | |_) | (_) | (_| | | | (_| |
|
||||||
|
\___/ \_/\_/ |_|____/ \___/ \__,_|_| \__,_|
|
||||||
|
|
||||||
|
_ _ _ _ _
|
||||||
|
| |_(_)_ __ ___ ___| |_ __ _| |__ | | ___ _ __ ___ __ _ _ __
|
||||||
|
| __| | '_ ' _ \ / _ \ __/ _' | '_ \| |/ _ \_____| '_ ' _ \ / _' | '__|
|
||||||
|
| |_| | | | | | | __/ || (_| | |_) | | __/_____| | | | | | (_| | |
|
||||||
|
\__|_|_| |_| |_|\___|\__\__,_|_.__/|_|\___| |_| |_| |_|\__, |_|
|
||||||
|
|___/
|
||||||
|
`
|
||||||
|
|
||||||
|
fmt.Println(art)
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user