Compare commits
2 Commits
5f6ded7cd4
...
887349d720
Author | SHA1 | Date |
---|---|---|
Sean Hickey | 887349d720 | |
Sean Hickey | edbda9aeb5 |
|
@ -1,7 +1,7 @@
|
||||||
# gopackagebase
|
# gopackagebase
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
go get git.wisellama.rocks/Wisellama/gopackagebase@v0.0.1
|
go get git.wisellama.rocks/Wisellama/gopackagebase@v0.0.4
|
||||||
```
|
```
|
||||||
|
|
||||||
This modules contains basic setup and initialization functions that
|
This modules contains basic setup and initialization functions that
|
||||||
|
|
|
@ -30,10 +30,11 @@ func Initialize(configFile string, defaultConfig gosimpleconf.ConfigMap) (*BaseC
|
||||||
log.Fatalf("error during configure: %v\n", err)
|
log.Fatalf("error during configure: %v\n", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
utcTime := gosimpleconf.Bool(configMap["log.utcTime"])
|
||||||
writeToFile := gosimpleconf.Bool(configMap["log.writeToFile"])
|
writeToFile := gosimpleconf.Bool(configMap["log.writeToFile"])
|
||||||
logFilename := configMap["log.file"]
|
logFilename := configMap["log.file"]
|
||||||
|
|
||||||
logWriter, err := SetupLogging(writeToFile, logFilename)
|
logWriter, err := SetupLogging(writeToFile, utcTime, logFilename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,31 @@
|
||||||
|
package gopackagebase_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"git.wisellama.rocks/Wisellama/gopackagebase"
|
||||||
|
"git.wisellama.rocks/Wisellama/gosimpleconf"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestGoPackageBase(t *testing.T) {
|
||||||
|
defaultConf := gosimpleconf.ConfigMap{
|
||||||
|
"log.writeToFile": "false",
|
||||||
|
}
|
||||||
|
|
||||||
|
baseConfig, err := gopackagebase.Initialize("idontexist.conf", defaultConf)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if baseConfig == nil {
|
||||||
|
t.Fatalf("baseConfig was nil")
|
||||||
|
}
|
||||||
|
|
||||||
|
value := baseConfig.ConfigMap["log.writeToFile"]
|
||||||
|
expected := defaultConf["log.writeToFile"]
|
||||||
|
if value != expected {
|
||||||
|
t.Errorf("incorrect value for log.writeToFile: expected %v, got %v", expected, value)
|
||||||
|
}
|
||||||
|
|
||||||
|
baseConfig.Cancel()
|
||||||
|
}
|
12
logging.go
12
logging.go
|
@ -8,20 +8,25 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type logWriter struct {
|
type logWriter struct {
|
||||||
|
utcTime bool
|
||||||
writeToFile bool
|
writeToFile bool
|
||||||
logFile *os.File
|
logFile *os.File
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *logWriter) Write(bytes []byte) (int, error) {
|
func (w *logWriter) Write(bytes []byte) (int, error) {
|
||||||
t := time.Now().UTC().Format(time.RFC3339)
|
t := time.Now()
|
||||||
return fmt.Fprintf(w.logFile, "%v %v", t, string(bytes))
|
if w.utcTime {
|
||||||
|
t = t.UTC()
|
||||||
|
}
|
||||||
|
format := t.Format(time.RFC3339)
|
||||||
|
return fmt.Fprintf(w.logFile, "%v %v", format, string(bytes))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *logWriter) Cleanup() {
|
func (w *logWriter) Cleanup() {
|
||||||
defer w.logFile.Close()
|
defer w.logFile.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
func SetupLogging(writeToFile bool, logFilename string) (*logWriter, error) {
|
func SetupLogging(writeToFile bool, utcTime bool, logFilename string) (*logWriter, error) {
|
||||||
var err error
|
var err error
|
||||||
log.SetFlags(0)
|
log.SetFlags(0)
|
||||||
|
|
||||||
|
@ -34,6 +39,7 @@ func SetupLogging(writeToFile bool, logFilename string) (*logWriter, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
writer := &logWriter{
|
writer := &logWriter{
|
||||||
|
utcTime: utcTime,
|
||||||
writeToFile: writeToFile,
|
writeToFile: writeToFile,
|
||||||
logFile: logFile,
|
logFile: logFile,
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue