File: logevent.go

package info (click to toggle)
golang-github-googlecloudplatform-guest-logging-go 0.0~git20210621.facc9c0-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 120 kB
  • sloc: makefile: 2
file content (103 lines) | stat: -rw-r--r-- 2,891 bytes parent folder | download
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
//  Copyright 2019 Google Inc. All Rights Reserved.
//
//  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 logger logs messages as appropriate.
package logger

import (
	"fmt"
	"path/filepath"
	"runtime"
	"strings"
	"time"

	logpb "google.golang.org/genproto/googleapis/logging/v2"
)

// Severity is the severity level of the log event.
type Severity int

// Log severity levels.
const (
	Debug Severity = iota
	Info
	Warning
	Error
	Critical
)

var severityName = map[Severity]string{
	Debug:    "Debug",
	Info:     "Info",
	Warning:  "Warning",
	Error:    "Error",
	Critical: "Critical",
}

func (v Severity) String() string {
	s, ok := severityName[v]
	if ok {
		return s
	}
	return ""
}

// LogEntry encapsulates a single log entry.
type LogEntry struct {
	Message string `json:"message"`
	// If present, this will be set as Payload to Cloud Logging
	// instead of Message and LocalTimeStamp.
	//
	// Note: Message is still sent to local logs.
	StructuredPayload interface{}       `json:"omitempty"`
	Labels            map[string]string `json:"-"`
	CallDepth         int               `json:"-"`
	Severity          Severity          `json:"-"`
	// Source will be overwritten, do not set.
	Source *logpb.LogEntrySourceLocation `json:"-"`
	// LocalTimestamp will be overwritten, do not set.
	LocalTimestamp string `json:"localTimestamp"`
}

func (e LogEntry) String() string {
	if formatFunction != nil {
		return formatFunction(e)
	}
	if e.Severity == Error || e.Severity == Critical {
		// 2006-01-02T15:04:05.999999Z07:00 LoggerName ERROR file.go:82: This is a log message.
		return fmt.Sprintf("%s %s %s %s:%d: %s", e.LocalTimestamp, loggerName, e.Severity, e.Source.File, e.Source.Line, e.Message)
	}
	// 2006-01-02T15:04:05.999999Z07:00 LoggerName INFO: This is a log message.
	return fmt.Sprintf("%s %s %s: %s", e.LocalTimestamp, loggerName, e.Severity, e.Message)
}

func (e LogEntry) bytes() []byte {
	return []byte(strings.TrimSpace(e.String()) + "\n")
}

func now() string {
	// RFC3339 with milliseconds.
	return time.Now().Format("2006-01-02T15:04:05.0000Z07:00")
}

func caller(depth int) *logpb.LogEntrySourceLocation {
	depth = depth + 1
	pc, file, line, ok := runtime.Caller(depth)
	if !ok {
		file = "???"
		line = 0
	}

	return &logpb.LogEntrySourceLocation{File: filepath.Base(file), Line: int64(line), Function: runtime.FuncForPC(pc).Name()}
}