-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog.go
More file actions
60 lines (54 loc) · 1.27 KB
/
log.go
File metadata and controls
60 lines (54 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
* Copyright 2023-2025 Thorsten A. Knieling
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*/
package mqtt2db
import (
"fmt"
"os"
"path/filepath"
"github.com/sirupsen/logrus"
"github.com/tknie/log"
"github.com/tknie/services"
)
var logRus = logrus.StandardLogger()
func Starlog() {
fileName := "mqtt2db.trace.log"
level := os.Getenv("ENABLE_MQTT2DB_DEBUG")
logLevel := logrus.WarnLevel
switch level {
case "debug", "1":
log.SetDebugLevel(true)
logLevel = logrus.DebugLevel
case "info", "2":
log.SetDebugLevel(false)
logLevel = logrus.InfoLevel
default:
}
logRus.SetFormatter(&logrus.TextFormatter{
FullTimestamp: true,
TimestampFormat: "2006-01-02T15:04:05",
})
logRus.SetLevel(logLevel)
p := os.Getenv("LOGPATH")
if p == "" {
p = os.TempDir()
}
path := filepath.FromSlash(p + string(os.PathSeparator) + fileName)
f, err := os.OpenFile(path,
os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)
if err != nil {
fmt.Println("Error opening log:", err)
return
}
logRus.SetOutput(f)
logRus.Infof("Init logrus")
log.Log = logRus
services.ServerMessage("Logging initiated ...")
}