-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.go
116 lines (95 loc) · 3.08 KB
/
options.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
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
// Package golog Simple flexible go logging
// This file contains logger configuration implementation
package golog
import (
"io"
"os"
)
// TODO:
// - Add support for loading from file JSON || YAML
// - Add support for loading from Environment Variables
// Environment enumeration
type Environment int
const (
// EnvAuto - No Environment set (initial) Will detect by looking for BUILD_ENV os variable
EnvAuto Environment = 0 + iota
// EnvDevelopment - All Log levels, color enabled and extra info on errors
// Log only these levels: All
EnvDevelopment
// EnvQuality - No debug level logging, color enabled, no extra info on errors
// Log only these levels: Info, Notice, Success, Warning, Error and RAW
EnvQuality
// EnvProduction - Error level & higher, no color, minimum information
// Log only these levels: Warning, Error and RAW
EnvProduction
)
// ColorMode enumeration
type ColorMode int
const (
// ClrNotSet - No color mode is set (initial)
ClrNotSet ColorMode = -1 + iota
// ClrDisabled - Do not use color. Overrides defaults
ClrDisabled
// ClrEnabled - Force use of color. Overrides defaults
ClrEnabled
// ClrAuto - Use color based on detected (or set) Environment
ClrAuto
)
// Options allow customization of the logger by the end user
type Options struct {
Module string // Name of running module
Environment Environment // Override default handling
UseColor ColorMode // Enable color (override) default handling
SmartError bool // Extended error that adapts by environment
Out io.Writer // Where to write output
FmtProd string // for use with production environment
FmtDev string // for use with development environment
Testing bool // This is set to true if go testing is detected
}
// NewDefaultOptions returns a new Options object with all defaults
func NewDefaultOptions() *Options {
return &Options{
Module: "unknown",
Environment: detectEnvironment(),
UseColor: ClrAuto,
SmartError: true,
Out: os.Stderr,
FmtProd: FmtProductionLog,
FmtDev: FmtDevelopmentLog,
}
}
// NewCustomOptions returns a new Options object with all user options
func NewCustomOptions(module string, env Environment, clr ColorMode, SmartError bool, out io.Writer, fmtProd, fmtDev string) *Options {
o := NewDefaultOptions()
// If given module is valid use it, otherwise keep default
if len(module) > 3 {
o.Module = module
}
if env == EnvProduction || env == EnvQuality || env == EnvDevelopment {
o.Environment = env
}
if clr != ClrNotSet {
o.UseColor = clr
}
o.SmartError = SmartError
if out != nil {
o.Out = out
}
if len(fmtProd) > 10 {
o.FmtProd = fmtProd
}
if len(fmtDev) > 10 {
o.FmtDev = fmtDev
}
return o
}
// EnvAsString returns the current envirnment for options as a string
func (o *Options) EnvAsString() string {
environments := [...]string{
"EvnAuto",
"EnvDevelopment",
"EnvQuality",
"EnvProduction",
}
return environments[o.Environment]
}