Dear Gitlab users, due to maintenance reasons, Gitlab will not be available on Thursday 30.09.2021 from 5:00 pm to approximately 5:30 pm.

Commit 848252cf authored by Andreas Wagner's avatar Andreas Wagner
Browse files

Logging almost fixed.

parent e2329b35
......@@ -54,11 +54,15 @@ Then, there are subsections at these keys: `Log`, `Zenodo`, `Git` and `Metadata`
### Log configuration
The `Log` sections contains a single `File` key-value pair, specifying the file that tei2zenodo should be writing its log entries to:
The `Log` sections contains a two entries:
- a `level` setting, specifying how much diagnostic informations should be logged. There are seven valid values: `trace`, `debug`, `info`, `warning`, `error`, `fatal` and `panic`
- a `file` setting, specifying the file that tei2zenodo should be writing its log entries to:
```json
"Log": {
"file": "t2z.log"
"file": "t2z.log",
"level": "Info"
}
```
......
package main
import (
"log"
"fmt"
"strconv"
// "github.com/davecgh/go-spew/spew"
"github.com/sirupsen/logrus"
"gitlab.gwdg.de/rg-mpg-de/tei2zenodo"
"gitlab.gwdg.de/rg-mpg-de/tei2zenodo/internal/pkg/conf"
"gitlab.gwdg.de/rg-mpg-de/tei2zenodo/internal/pkg/logger"
"gitlab.gwdg.de/rg-mpg-de/tei2zenodo/internal/pkg/routing"
)
// Config stores the application's configuration.
var Config tei2zenodo.Config
var log logrus.Logger
func main() {
var Config tei2zenodo.Config
conf.Configure(&Config)
Cerr := conf.Configure(&Config)
if Cerr != nil {
panic(fmt.Errorf("Error during config phase_ %+v", Cerr))
return
}
log, Lerr := logger.ConfigureLogging(&Config.Log)
if Lerr != nil {
panic(fmt.Errorf("Error during log setup phase: %+v", Lerr))
return
}
log.Printf("Starting tei2zenodo daemon")
// log.Printf("[main/main] Config: %+v", Config)
......
......@@ -6,7 +6,7 @@
"WebhookAPI": "/hooks/receivers/github/events/",
"Log": {
"file": "t2z.log",
"level": "InfoLevel"
"level": "Info"
},
"Zenodo": {
"prefix": "10.5072/zenodo.",
......
package conf
import (
"log"
"fmt"
"strings"
"github.com/spf13/viper"
"gitlab.gwdg.de/rg-mpg-de/tei2zenodo"
"gitlab.gwdg.de/rg-mpg-de/tei2zenodo/internal/pkg/logger"
)
// Configure loads configuration parameters into Config struct
func Configure(Config *tei2zenodo.Config) error {
// err := viper.BindPFlags(cmd.Flags())
// if err != nil {
// return err
// }
/* Enable commandline switches:
err := viper.BindPFlags(cmd.Flags())
if err != nil {
return err
}
*/
// set defaults
viper.SetDefault("ContentDir", "content")
viper.SetDefault("LayoutDir", "layouts")
viper.SetDefault("Taxonomies", map[string]string{"tag": "tags", "category": "categories"})
viper.SetEnvPrefix("T2Z")
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.AutomaticEnv()
// if configFile, _ := cmd.Flags().GetString("config"); configFile != "" {
// viper.SetConfigFile(configFile)
// } else {
// set configFile
viper.SetConfigName("config")
viper.SetConfigType("json")
viper.AddConfigPath("./")
viper.AddConfigPath("./configs/")
viper.AddConfigPath("$HOME/.t2z")
// }
if err := viper.ReadInConfig(); err != nil {
log.Printf("conf/Configure - Cannot read config in %s: %+v", viper.ConfigFileUsed(), err)
return err
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
panic(fmt.Errorf("Config file not found"))
return err
} else {
panic(fmt.Errorf("Cannot read config in %s: %+v", viper.ConfigFileUsed(), err))
return err
}
}
logger.ConfigureLogging(&Config.Log)
viper.Unmarshal(Config)
/*
viper.WatchConfig()
viper.OnConfigChange(func(e fsnotify.Event) {
fmt.Println("Config file changed:", e.Name)
})
*/
err := viper.Unmarshal(Config)
if err != nil {
panic(fmt.Errorf("Cannot parse config: %+v", err))
return err
}
return nil
}
......@@ -8,13 +8,14 @@ import (
"fmt"
"io"
"io/ioutil"
"log"
"mime/multipart"
"net/http"
"strconv"
"strings"
"time"
log "github.com/sirupsen/logrus"
"gitlab.gwdg.de/rg-mpg-de/tei2zenodo"
)
......
package logger
import (
"bufio"
"io"
"os"
"strings"
......@@ -10,27 +10,30 @@ import (
)
// ConfigureLogging will take the logging configuration and also adds a few default parameters.
func ConfigureLogging(config *tei2zenodo.LoggingConfig) (*logrus.Entry, error) {
hostname, err := os.Hostname()
if err != nil {
return nil, err
}
func ConfigureLogging(config *tei2zenodo.LoggingConfig) (*logrus.Logger, error) {
/*
hostname, err := os.Hostname()
if err != nil {
return nil, err
}
*/
// use a file if you want
if config.File != "" {
f, errOpen := os.OpenFile(config.File, os.O_RDWR|os.O_APPEND, 0660)
f, errOpen := os.OpenFile(config.File, os.O_CREATE|os.O_RDWR|os.O_APPEND, 0660)
if errOpen != nil {
return nil, errOpen
}
logrus.SetOutput(bufio.NewWriter(f))
// logrus.SetOutput(bufio.NewWriter(f))
logrus.SetOutput(io.MultiWriter(f, os.Stdout))
}
if config.Level != "" {
level, err := logrus.ParseLevel(strings.ToUpper(config.Level))
if err != nil {
return nil, err
l, errParse := logrus.ParseLevel(strings.ToUpper(config.Level))
if errParse != nil {
return nil, errParse
}
logrus.SetLevel(level)
logrus.SetLevel(l)
}
// always use the fulltimestamp
......@@ -39,5 +42,5 @@ func ConfigureLogging(config *tei2zenodo.LoggingConfig) (*logrus.Entry, error) {
DisableTimestamp: false,
})
return logrus.StandardLogger().WithField("hostname", hostname), nil
return logrus.StandardLogger(), nil
}
......@@ -4,11 +4,12 @@ import (
"bytes"
"fmt"
"io"
"log"
"net/http"
"strconv"
"strings"
log "github.com/sirupsen/logrus"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/stephenmuss/ginerus"
......
......@@ -3,11 +3,12 @@ package t2zxml
import (
"fmt"
"io"
"log"
"reflect"
"regexp"
"strings"
log "github.com/sirupsen/logrus"
"github.com/antchfx/xmlquery"
"github.com/antchfx/xpath"
"github.com/beevik/etree"
......
......@@ -6,12 +6,13 @@ import (
"fmt"
"io"
"io/ioutil"
"log"
"mime/multipart"
"net/http"
"strconv"
"strings"
log "github.com/sirupsen/logrus"
"gitlab.gwdg.de/rg-mpg-de/tei2zenodo"
"gitlab.gwdg.de/rg-mpg-de/tei2zenodo/internal/pkg/t2zxml"
)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment