-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.go
More file actions
141 lines (122 loc) · 3.47 KB
/
logger.go
File metadata and controls
141 lines (122 loc) · 3.47 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
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
//
// Copyright (C) 2021 - 2025 Dmitry Kolesnikov
//
// This file may be modified and distributed under the terms
// of the MIT license. See the LICENSE file for details.
// https://github.com/fogfish/logger
//
// Package logger configures slog for AWS CloudWatch
package logger
import (
"log/slog"
"os"
)
// Create New Logger
func New(opts ...Option) *slog.Logger {
if _, has := os.LookupEnv("AWS_LAMBDA_FUNCTION_NAME"); has {
return slog.New(NewJSONHandler(opts...))
}
if preset, has := os.LookupEnv("CONFIG_LOG_PROFILE"); has {
switch preset {
case "CloudWatch":
return slog.New(NewJSONHandler(opts...))
default:
return slog.New(NewStdioHandler(opts...))
}
}
return slog.New(NewStdioHandler(opts...))
}
const (
// EMERGENCY
// system is unusable, panic execution of current routine/application,
// it is notpossible to gracefully terminate it.
EMERGENCY = slog.Level(100)
EMR = EMERGENCY
// CRITICAL
// system is failed, response actions must be taken immediately,
// the application is not able to execute correctly but still
// able to gracefully exit.
CRITICAL = slog.Level(50)
CRT = CRITICAL
// ERROR
// system is failed, unable to recover from error.
// The failure do not have global catastrophic impacts but
// local functionality is impaired, incorrect result is returned.
ERROR = slog.LevelError
ERR = ERROR
// WARN
// system is failed, unable to recover, degraded functionality.
// The failure is ignored and application still capable to deliver
// incomplete but correct results.
WARN = slog.LevelWarn
WRN = WARN
// NOTICE
// system is failed, error is recovered, no impact
NOTICE = slog.Level(2)
NTC = NOTICE
// INFO
// output informative status about system
INFO = slog.LevelInfo
INF = INFO
// DEBUG
// output debug status about system
DEBUG = slog.LevelDebug
DEB = DEBUG
)
var (
levelShortName = map[slog.Level]string{
EMERGENCY: "EMR",
CRITICAL: "CRT",
ERROR: "ERR",
WARN: "WRN",
NOTICE: "NTC",
INFO: "INF",
DEBUG: "DEB",
}
levelLongName = map[slog.Level]string{
EMERGENCY: "EMERGENCY",
CRITICAL: "CRITICAL",
ERROR: "ERROR",
WARN: "WARN",
NOTICE: "NOTICE",
INFO: "INFO",
DEBUG: "DEBUG",
}
colorReset = "\x1b[0m"
levelColorForName = map[slog.Level]string{
EMERGENCY: "\x1b[1;48;5;198m", // Red (intensive)
CRITICAL: "\x1b[48;5;160m", // Red (mid-intensive)
ERROR: "\x1b[1;31m", // Red
WARN: "\x1b[1;38;5;178m", // Orange
NOTICE: "\x1b[0;38;5;111m", // Blue
INFO: "\x1b[38;5;255m", // White
DEBUG: "\x1b[38;5;248m", // Grey
}
levelColorForText = map[slog.Level]string{
EMERGENCY: "\x1b[1;38;5;198m", // Red
CRITICAL: "\x1b[0;38;5;160m", // Red
ERROR: "\x1b[38;5;15m", // White
WARN: "\x1b[38;5;15m", // White
NOTICE: "\x1b[38;5;15m", // White
INFO: "\x1b[38;5;15m", // White
DEBUG: "\x1b[0m", // default
}
levelColorForAttr = map[slog.Level]string{
EMERGENCY: "\x1b[38;5;255m", // White
CRITICAL: "\x1b[38;5;255m", // White
ERROR: "\x1b[38;5;250m", // Grey
WARN: "\x1b[38;5;244m", // Drak Gray
NOTICE: "\x1b[38;5;244m", // Dark Gray
INFO: "\x1b[90m", // Darker Gray
DEBUG: "\x1b[90m", // Darker Gray
}
longNames = map[string]slog.Level{
"EMERGENCY": EMERGENCY,
"CRITICAL": CRITICAL,
"ERROR": ERROR,
"WARN": WARN,
"NOTICE": NOTICE,
"INFO": INFO,
"DEBUG": DEBUG,
}
)