-
-
Notifications
You must be signed in to change notification settings - Fork 514
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #449 from cupen/patch-logs
Improved logs.
- Loading branch information
Showing
11 changed files
with
121 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,77 @@ | ||
package log | ||
|
||
type optionFn func() | ||
import ( | ||
"os" | ||
) | ||
|
||
var ( | ||
Development = &Options{ | ||
logLevel: DebugLevel, | ||
enableCaller: true, | ||
} | ||
|
||
Production = &Options{ | ||
logLevel: InfoLevel, | ||
enableCaller: false, | ||
} | ||
|
||
Current = Production | ||
) | ||
|
||
func init() { | ||
env := os.Getenv("PROTO_ACTOR_ENV") | ||
switch env { | ||
case "dev": | ||
Current = Development | ||
case "prod": | ||
Current = Production | ||
default: | ||
Current = Production | ||
} | ||
} | ||
|
||
// Options for log. | ||
type Options struct { | ||
logLevel Level | ||
enableCaller bool | ||
} | ||
|
||
// Setup is used to configure the log system | ||
func (o *Options) With(opts ...option) *Options { | ||
cloned := *o | ||
for _, opt := range opts { | ||
opt(&cloned) | ||
} | ||
return &cloned | ||
} | ||
|
||
type option func(*Options) | ||
|
||
// WithEventSubscriber option replaces the default Event subscriber with fn. | ||
// | ||
// Specifying nil will disable logging of events. | ||
func WithEventSubscriber(fn func(evt Event)) optionFn { | ||
return func() { | ||
if sub != nil { | ||
Unsubscribe(sub) | ||
} | ||
if fn != nil { | ||
sub = Subscribe(fn) | ||
} | ||
func WithEventSubscriber(fn func(evt Event)) option { | ||
return func(opts *Options) { | ||
resetEventSubscriber(fn) | ||
} | ||
} | ||
|
||
// SetOptions is used to configure the log system | ||
func SetOptions(opts ...optionFn) { | ||
for _, opt := range opts { | ||
opt() | ||
// WithCaller option will print the file name and line number. | ||
func WithCaller(enabled bool) option { | ||
return func(opts *Options) { | ||
opts.enableCaller = enabled | ||
} | ||
} | ||
|
||
func WithDefaultLevel(level Level) option { | ||
if level == DefaultLevel { | ||
level = InfoLevel | ||
} | ||
return func(opts *Options) { | ||
opts.logLevel = level | ||
} | ||
} | ||
|
||
func SetOptions(opts ...option) { | ||
Current = Current.With(opts...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters