Reorganise logging
This commit is contained in:
@@ -28,9 +28,9 @@ func PushVersionToDb() {
|
||||
coll := MongoClient.Database("owlboard").Collection("versions")
|
||||
_, err := coll.UpdateOne(context.TODO(), versionSelector, bson.M{"$set": version}, opts)
|
||||
if err != nil {
|
||||
log.Msg.Warn("Unable to push version to database: " + err.Error())
|
||||
log.Warn("Unable to push version to database: " + err.Error())
|
||||
} else {
|
||||
log.Msg.Debug("Version up to date in Database")
|
||||
log.Debug("Version up to date in Database")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@ func PutOneService(data database.Service) bool {
|
||||
coll := MongoClient.Database(databaseName).Collection(timetableCollection)
|
||||
_, err := coll.InsertOne(context.TODO(), data)
|
||||
if err != nil {
|
||||
log.Msg.Error("Unable to insert to database: " + err.Error())
|
||||
log.Error("Unable to insert to database: " + err.Error())
|
||||
return false
|
||||
}
|
||||
return true
|
||||
@@ -55,7 +55,7 @@ func DeleteOneService(data database.DeleteQuery) bool {
|
||||
}
|
||||
_, err := coll.DeleteOne(context.TODO(), filter)
|
||||
if err != nil {
|
||||
log.Msg.Error("Unable to delete service: " + err.Error())
|
||||
log.Error("Unable to delete service: " + err.Error())
|
||||
return false
|
||||
}
|
||||
return true
|
||||
|
||||
@@ -36,11 +36,11 @@ func GetCifMetadata() (*CifMetadata, error) {
|
||||
if errors.Is(err, mongo.ErrNoDocuments) {
|
||||
return nil, nil
|
||||
}
|
||||
log.Msg.Error("Error fetching CIF Metadata")
|
||||
log.Error("Error fetching CIF Metadata")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
log.Msg.Debug("Fetched CIF Metadata from database", zap.Any("Metadata", result))
|
||||
log.Debug("Fetched CIF Metadata from database", zap.Any("Metadata", result))
|
||||
|
||||
return &result, nil
|
||||
}
|
||||
@@ -64,11 +64,11 @@ func PutCifMetadata(metadata *CifMetadata, lastUpdateType string) bool {
|
||||
_, err := collection.UpdateOne(context.Background(), filter, update, options)
|
||||
|
||||
if err != nil {
|
||||
log.Msg.Error("Error updating CIF Metadata", zap.Error(err))
|
||||
log.Error("Error updating CIF Metadata", zap.Error(err))
|
||||
return false
|
||||
}
|
||||
|
||||
log.Msg.Info("New CIF Metadata written", zap.Time("Update time", metadata.LastUpdate))
|
||||
log.Info("New CIF Metadata written", zap.Time("Update time", metadata.LastUpdate))
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -76,15 +76,15 @@ func PutCifMetadata(metadata *CifMetadata, lastUpdateType string) bool {
|
||||
func DeleteCifEntries(deletions []database.DeleteQuery) error {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
log.Msg.Panic("Panic:", zap.Any("panic", r))
|
||||
log.Panic("Panic:", zap.Any("panic", r))
|
||||
}
|
||||
}()
|
||||
// Skip if deletions is empty
|
||||
if len(deletions) == 0 {
|
||||
log.Msg.Info("No deletions required")
|
||||
log.Info("No deletions required")
|
||||
return nil
|
||||
}
|
||||
log.Msg.Info("Running deletions against database", zap.Int("count", len(deletions)))
|
||||
log.Info("Running deletions against database", zap.Int("count", len(deletions)))
|
||||
|
||||
// Prepare deletion tasks
|
||||
collection := MongoClient.Database(databaseName).Collection(timetableCollection)
|
||||
@@ -103,7 +103,7 @@ func DeleteCifEntries(deletions []database.DeleteQuery) error {
|
||||
|
||||
_, err := collection.BulkWrite(context.Background(), bulkDeletions, bulkWriteOptions)
|
||||
if err != nil {
|
||||
log.Msg.Error("Error deleting documents", zap.Error(err))
|
||||
log.Error("Error deleting documents", zap.Error(err))
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -114,10 +114,10 @@ func DeleteCifEntries(deletions []database.DeleteQuery) error {
|
||||
func CreateCifEntries(schedules []database.Service) error {
|
||||
// Skip if deletions is empty
|
||||
if len(schedules) == 0 {
|
||||
log.Msg.Info("No creations required")
|
||||
log.Info("No creations required")
|
||||
return nil
|
||||
}
|
||||
log.Msg.Info("Running creations against database", zap.Int("count", len(schedules)))
|
||||
log.Info("Running creations against database", zap.Int("count", len(schedules)))
|
||||
|
||||
collection := MongoClient.Database(databaseName).Collection(timetableCollection)
|
||||
|
||||
@@ -132,7 +132,7 @@ func CreateCifEntries(schedules []database.Service) error {
|
||||
|
||||
_, err := collection.BulkWrite(context.Background(), models, bulkWriteOptions)
|
||||
if err != nil {
|
||||
log.Msg.Error("Error inserting documents", zap.Error(err))
|
||||
log.Error("Error inserting documents", zap.Error(err))
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
@@ -34,9 +34,9 @@ func InitDataAccess(cfg *helpers.Configuration) {
|
||||
client, err := mongo.Connect(ctx, options.Client().ApplyURI(uri).SetBSONOptions(bsonOpts))
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
log.Msg.Fatal("Error connecting to database: " + err.Error())
|
||||
log.Fatal("Error connecting to database: " + err.Error())
|
||||
} else {
|
||||
log.Msg.Info("Database connection successful")
|
||||
log.Info("Database connection successful")
|
||||
}
|
||||
MongoClient = client
|
||||
}
|
||||
@@ -47,9 +47,9 @@ func CloseMongoClient() {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
if err := MongoClient.Disconnect(ctx); err != nil {
|
||||
log.Msg.Warn("Error disconnecting MongoDB client: " + err.Error())
|
||||
log.Warn("Error disconnecting MongoDB client: " + err.Error())
|
||||
} else {
|
||||
log.Msg.Info("MongoDB client disconnected.")
|
||||
log.Info("MongoDB client disconnected.")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,13 +12,13 @@ import (
|
||||
|
||||
// CAUTION: Drops the collection named in collectionName
|
||||
func DropCollection(collectionName string) error {
|
||||
log.Msg.Info("Dropping collection", zap.String("Collection Name", collectionName))
|
||||
log.Info("Dropping collection", zap.String("Collection Name", collectionName))
|
||||
database := MongoClient.Database(databaseName)
|
||||
collection := database.Collection(collectionName)
|
||||
|
||||
err := collection.Drop(context.Background())
|
||||
if err != nil {
|
||||
log.Msg.Error("Error dropping collection", zap.String("Collection Name", collectionName), zap.Error(err))
|
||||
log.Error("Error dropping collection", zap.String("Collection Name", collectionName), zap.Error(err))
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -45,7 +45,7 @@ func CheckUpdateTime(collectionName string) (int64, error) {
|
||||
|
||||
// Sets a new update time (unix timestamp) of the collection named in collectionName. The update time is calculated within the function.
|
||||
func SetUpdateTime(collectionName string) error {
|
||||
log.Msg.Info("Setting update time", zap.String("collection", collectionName))
|
||||
log.Info("Setting update time", zap.String("collection", collectionName))
|
||||
database := MongoClient.Database(databaseName)
|
||||
collection := database.Collection("meta")
|
||||
options := options.Update().SetUpsert(true)
|
||||
@@ -65,7 +65,7 @@ func SetUpdateTime(collectionName string) error {
|
||||
_, err := collection.UpdateOne(context.Background(), filter, update, options)
|
||||
|
||||
if err != nil {
|
||||
log.Msg.Error("Error setting update time", zap.String("collection", collectionName), zap.Error(err))
|
||||
log.Error("Error setting update time", zap.String("collection", collectionName), zap.Error(err))
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user