Skip to content

Commit 93a3b74

Browse files
Introduce syslogger to be used with STE (#1721)
* Introduce syslogger to be used with STE * Fix windows build * Address reviews
1 parent 832fa4c commit 93a3b74

File tree

1 file changed

+118
-0
lines changed

1 file changed

+118
-0
lines changed

common/logger_unix.go

+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
// +build linux darwin
2+
// Copyright Microsoft <wastore@microsoft.com>
3+
//
4+
// Permission is hereby granted, free of charge, to any person obtaining a copy
5+
// of this software and associated documentation files (the "Software"), to deal
6+
// in the Software without restriction, including without limitation the rights
7+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
// copies of the Software, and to permit persons to whom the Software is
9+
// furnished to do so, subject to the following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included in
12+
// all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
// THE SOFTWARE.
21+
22+
package common
23+
24+
import (
25+
"fmt"
26+
"runtime"
27+
"log/syslog"
28+
29+
"github.com/Azure/azure-pipeline-go/pipeline"
30+
)
31+
32+
//////////////////////////////////////////
33+
type sysLogger struct {
34+
// minimum loglevel represents the minimum severity of log messages which can be logged to Job Log file.
35+
// any message with severity higher than this will be ignored.
36+
jobID JobID
37+
minimumLevelToLog pipeline.LogLevel // The maximum customer-desired log level for this job
38+
writer *syslog.Writer // The Job's logger
39+
logSuffix string
40+
sanitizer pipeline.LogSanitizer
41+
}
42+
43+
44+
func NewSysLogger(jobID JobID, minimumLevelToLog LogLevel, logSuffix string) ILoggerResetable {
45+
return &sysLogger{
46+
jobID: jobID,
47+
minimumLevelToLog: minimumLevelToLog.ToPipelineLogLevel(),
48+
logSuffix: logSuffix,
49+
sanitizer: NewAzCopyLogSanitizer(),
50+
}
51+
}
52+
53+
func (sl *sysLogger) OpenLog() {
54+
if sl.minimumLevelToLog == pipeline.LogNone {
55+
return
56+
}
57+
writer, err := syslog.New(syslog.LOG_NOTICE, fmt.Sprintf("%s %s", sl.logSuffix, sl.jobID.String()))
58+
PanicIfErr(err)
59+
60+
sl.writer = writer
61+
// Log the Azcopy Version
62+
sl.writer.Notice("AzcopyVersion " + AzcopyVersion)
63+
// Log the OS Environment and OS Architecture
64+
sl.writer.Notice("OS-Environment " + runtime.GOOS)
65+
sl.writer.Notice("OS-Architecture " + runtime.GOARCH)
66+
}
67+
68+
func (sl *sysLogger) MinimumLogLevel() pipeline.LogLevel {
69+
return sl.minimumLevelToLog
70+
}
71+
72+
func (jl *sysLogger) ShouldLog(level pipeline.LogLevel) bool {
73+
if level == pipeline.LogNone {
74+
return false
75+
}
76+
return level <= jl.minimumLevelToLog
77+
}
78+
79+
func (sl *sysLogger) CloseLog() {
80+
if sl.minimumLevelToLog == pipeline.LogNone {
81+
return
82+
}
83+
84+
sl.writer.Notice("Closing Log")
85+
sl.writer.Close()
86+
}
87+
88+
89+
func (sl *sysLogger) Panic(err error) {
90+
sl.writer.Crit(err.Error()) // We do NOT panic here as the app would terminate;
91+
//we just log it. We should never reach this line of code!
92+
}
93+
94+
func (sl *sysLogger) Log(loglevel pipeline.LogLevel, msg string) {
95+
if !sl.ShouldLog(loglevel) {
96+
return
97+
}
98+
w := sl.writer
99+
// ensure all secrets are redacted
100+
msg = sl.sanitizer.SanitizeLogMessage(msg)
101+
102+
switch loglevel {
103+
case pipeline.LogNone:
104+
//nothing to do
105+
case pipeline.LogFatal:
106+
w.Emerg(msg)
107+
case pipeline.LogPanic:
108+
w.Crit(msg)
109+
case pipeline.LogError:
110+
w.Err(msg)
111+
case pipeline.LogWarning:
112+
w.Warning(msg)
113+
case pipeline.LogInfo:
114+
w.Info(msg)
115+
case pipeline.LogDebug:
116+
w.Debug(msg)
117+
}
118+
}

0 commit comments

Comments
 (0)