-
Notifications
You must be signed in to change notification settings - Fork 114
/
logging.go
42 lines (34 loc) · 1.04 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
// Copyright (c) OpenFaaS Author(s) 2021. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
package executor
import (
"bufio"
"io"
"log"
)
// bindLoggingPipe spawns a goroutine for passing through logging of the given output pipe.
func bindLoggingPipe(name string, pipe io.Reader, output io.Writer, logPrefix bool, maxBufferSize int) {
log.Printf("Started logging: %s from function.", name)
scanner := bufio.NewScanner(pipe)
buffer := make([]byte, maxBufferSize)
scanner.Buffer(buffer, maxBufferSize)
logFlags := log.Flags()
prefix := log.Prefix()
if logPrefix == false {
logFlags = 0
prefix = "" // Unnecessary, but set explicitly for completeness.
}
logger := log.New(output, prefix, logFlags)
go func() {
for scanner.Scan() {
if logPrefix {
logger.Printf("%s: %s", name, scanner.Text())
} else {
logger.Print(scanner.Text())
}
}
if err := scanner.Err(); err != nil {
log.Printf("Error scanning %s: %s", name, err.Error())
}
}()
}