forked from aws/aws-lambda-runtime-interface-emulator
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtaillog.go
More file actions
52 lines (42 loc) · 1.05 KB
/
taillog.go
File metadata and controls
52 lines (42 loc) · 1.05 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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package logging
import (
"io"
"sync"
)
// TailLogWriter writes tail/debug log to provided io.Writer
type TailLogWriter struct {
out io.Writer
enabled bool
mutex sync.Mutex
}
// Enable enables log writer.
func (lw *TailLogWriter) Enable() {
lw.mutex.Lock()
defer lw.mutex.Unlock()
lw.enabled = true
}
// Disable disables log writer.
func (lw *TailLogWriter) Disable() {
lw.mutex.Lock()
defer lw.mutex.Unlock()
lw.enabled = false
}
// Writer wraps the basic io.Write method
func (lw *TailLogWriter) Write(p []byte) (n int, err error) {
lw.mutex.Lock()
defer lw.mutex.Unlock()
if lw.enabled {
return lw.out.Write(p)
}
// Else returns a successful write so that MultiWriter won't stop
return len(p), nil
}
// NewTailLogWriter returns a new invoke tail log writer, default output is discarded until output is configured.
func NewTailLogWriter(w io.Writer) *TailLogWriter {
return &TailLogWriter{
out: w,
enabled: false,
}
}