-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.py
86 lines (62 loc) · 2.17 KB
/
logger.py
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
# package pkl
# import (
# "fmt"
# "io"
# "os"
# )
# // Logger is the interface for logging messages emitted by the Pkl evaluator.
# //
# // To set a logger, register it on EvaluatorOptions.Logger when building an Evaluator.
# type Logger interface {
# // Trace logs the given message on level TRACE.
# Trace(message string, frameUri string)
# // Warn logs the given message on level WARN.
# Warn(message string, frameUri string)
# }
# // NewLogger builds a logger that writes to the provided output stream,
# // using the default formatting.
# func NewLogger(out io.Writer) Logger {
# return &logger{out}
# }
# // FormatLogMessage returns the default formatter for log messages.
# func FormatLogMessage(level, message, frameUri string) string {
# return fmt.Sprintf("pkl: %s: %s (%s)\n", level, message, frameUri)
# }
# type logger struct {
# out io.Writer
# }
# func (s logger) Trace(message string, frameUri string) {
# _, _ = s.out.Write([]byte(FormatLogMessage("TRACE", message, frameUri)))
# }
# func (s logger) Warn(message string, frameUri string) {
# _, _ = s.out.Write([]byte(FormatLogMessage("WARN", message, frameUri)))
# }
# var _ Logger = (*logger)(nil)
# // StderrLogger is a logger that writes to standard error.
# //
# //goland:noinspection GoUnusedGlobalVariable
# var StderrLogger = NewLogger(os.Stdout)
# // NoopLogger is a logger that discards all messages.
# var NoopLogger = NewLogger(io.Discard)
import dataclasses
import sys
from typing import TextIO
@dataclasses.dataclass
class Logger:
out: TextIO | None
def trace(self, message: str, frame_uri: str):
if self.out is None:
return
self.out.write(self.format_log_message("TRACE", message, frame_uri))
def warn(self, message: str, frame_uri: str):
if self.out is None:
return
self.out.write(self.format_log_message("WARN", message, frame_uri))
def format_log_message(self, level: str, message: str, frame_uri: str):
return f"pkl: {level}: {message} ({frame_uri})\n"
def new_logger(out: TextIO | None):
return Logger(out)
def stderr_logger():
return new_logger(sys.stderr)
def noop_logger():
return new_logger(None)