forked from scionproto/scion
/
logging.go
158 lines (143 loc) · 4.22 KB
/
logging.go
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Copyright 2018 ETH Zurich, Anapaya Systems
//
// 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
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package env
import (
"fmt"
"io"
"os"
"path/filepath"
"github.com/scionproto/scion/go/lib/common"
"github.com/scionproto/scion/go/lib/config"
"github.com/scionproto/scion/go/lib/log"
"github.com/scionproto/scion/go/lib/util"
)
// Startup* variables are set during link time.
var (
// The value is generated by tools/git-version.
StartupVersion string
)
var _ (config.Config) = (*Logging)(nil)
type Logging struct {
config.NoValidator
File struct {
// Path is the location of the logging file. If unset, no file logging
// is performed.
Path string
// Level of file logging (defaults to lib/log default).
Level string
// Size is the max size of log file in MiB (defaults to lib/log default).
Size uint
// MaxAge is the max age of log file in days (defaults to lib/log default).
MaxAge uint
// MaxBackups is the max number of log files to retain (defaults to lib/log default).
MaxBackups uint
// FlushInterval specifies how frequently to flush to the log file,
// in seconds (defaults to lib/log default).
FlushInterval *int
}
Console struct {
// Level of console logging (defaults to lib/log default).
Level string
}
}
// InitDefaults populates unset fields in cfg to their default values (if they
// have one).
func (cfg *Logging) InitDefaults() {
if cfg.Console.Level == "" {
cfg.Console.Level = log.DefaultConsoleLevel
}
if cfg.File.Level == "" {
cfg.File.Level = log.DefaultFileLevel
}
if cfg.File.Size == 0 {
cfg.File.Size = log.DefaultFileSizeMiB
}
if cfg.File.MaxAge == 0 {
cfg.File.MaxAge = log.DefaultFileMaxAgeDays
}
if cfg.File.MaxBackups == 0 {
cfg.File.MaxBackups = log.DefaultFileMaxBackups
}
if cfg.File.FlushInterval == nil {
s := log.DefaultFileFlushSeconds
cfg.File.FlushInterval = &s
}
}
func (cfg *Logging) Sample(dst io.Writer, path config.Path, ctx config.CtxMap) {
config.WriteSample(dst, path, nil,
config.StringSampler{
Text: fmt.Sprintf(loggingFileSample, ctx[config.ID]),
Name: "file",
},
config.StringSampler{
Text: loggingConsoleSample,
Name: "console",
},
)
}
func (cfg *Logging) ConfigName() string {
return "logging"
}
// InitLogging initializes logging and sets the root logger Log.
func InitLogging(cfg *Logging) error {
cfg.InitDefaults()
if err := setupFileLogging(cfg); err != nil {
return err
}
if err := log.SetupLogConsole(cfg.Console.Level); err != nil {
return err
}
return nil
}
func setupFileLogging(cfg *Logging) error {
if cfg.File.Path != "" {
return log.SetupLogFile(
filepath.Base(cfg.File.Path),
filepath.Dir(cfg.File.Path),
cfg.File.Level,
int(cfg.File.Size),
int(cfg.File.MaxAge),
int(cfg.File.MaxBackups),
*cfg.File.FlushInterval,
)
}
return nil
}
// LogAppStarted should be called by applications as soon as logging is
// initialized.
func LogAppStarted(svcType, elemId string) error {
inDocker, err := util.RunsInDocker()
if err != nil {
return common.NewBasicError("Unable to determine if running in docker", err)
}
info := fmt.Sprintf("=====================> Service started %s %s\n"+
"%s %s\n %s\n %s\n %s\n",
svcType,
elemId,
VersionInfo(),
fmt.Sprintf("In docker: %v", inDocker),
fmt.Sprintf("pid: %d", os.Getpid()),
fmt.Sprintf("euid/egid: %d %d", os.Geteuid(), os.Getegid()),
fmt.Sprintf("cmd line: %q", os.Args),
)
log.Info(info)
return nil
}
// VersionInfo returns build version information (build date, build version, build chain).
func VersionInfo() string {
return fmt.Sprintf(" Scion version: %s\n", StartupVersion)
}
func LogAppStopped(svcType, elemId string) {
log.Info(fmt.Sprintf("=====================> Service stopped %s %s", svcType, elemId))
}