-
Notifications
You must be signed in to change notification settings - Fork 0
/
factory.go
44 lines (35 loc) · 1.08 KB
/
factory.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
43
44
package cannon
import (
"errors"
"go.uber.org/zap"
)
var factory LogFactory
// LogFactoryFunc turns a function into a LogFactory
type LogFactoryFunc func() (*zap.Logger, error)
// LogFactory describes how to create a new logger
type LogFactory interface {
New() (*zap.Logger, error)
}
// New will return a new logger from the log factory function
func (f LogFactoryFunc) New() (*zap.Logger, error) {
return f()
}
// NewFactoryLogger will use the registered log factory to create a new logger
func NewFactoryLogger() (*zap.Logger, error) {
switch {
case factory == nil:
return nil, errors.New("no factory function defined, cannot create logger")
default:
return factory.New()
}
}
// RegisterFactory will register a global log factory that will be used for logger
// creation when `NewLogger()` is called. This is useful when you want customize how
// the logger is created, such as creating different loggers for development and prod.
func RegisterFactory(f LogFactory) {
factory = f
}
// ClearFactory will remove any registered log factory function
func ClearFactory() {
factory = nil
}