2015-09-11 15:27:36 +00:00
|
|
|
package log
|
|
|
|
|
2021-02-16 20:31:50 +00:00
|
|
|
//go:generate go run github.com/v2fly/v2ray-core/v4/common/errors/errorgen
|
2017-04-08 23:43:25 +00:00
|
|
|
|
2015-09-11 15:27:36 +00:00
|
|
|
import (
|
2017-02-01 20:35:40 +00:00
|
|
|
"context"
|
improve commands (#648)
* stat show json refactor
> will show {} since scripts should expect it, its the json style of blank value
* combine statistics commands to one
* code optimize
* fix runtime flag
* remove json indent
* change overridden to override
* api bi -json
* convert stdin support
code optimize
* writeRow() code optimize
add stats tittle
revert back to restartlogger
* api log -restart
* follow log
* codeql
* move -json to shared flags
* flags optimize
* update flag descriptions
* change "-v" of "api bo" to duration
* change "-expire" of "tls cert" to days
* cmds short description optimize
* fix multiple log followers
* Format loader refactor
* "infra/conf/merge" refactor
* "LoadConfig" refactor
* add "infra/conf/mergers"
* contribute to it will benifit `v2ray run`,`v2ray test`,`v2ray convert`
* easily add new formats, by just adding a converter like json.FromTOML
* default format auto, to all cmds above
* auto detect input format
* mixed formats support
* better stdin behavior
* don't wait if no content
* don't use 'stdin:' placeholder
* `v2ray test` now behaves exactly the same with `v2ray run`, including stdin reading
* api ado, adi, rmo, rmi refactor
* support folders to files resolving, mixed formats
* remove remaining 'stdin:' placeholders
* fix tests
* os.Stdin.Stat() behaves different in platforms, removed
* code optimize
Co-authored-by: loyalsoldier <10487845+Loyalsoldier@users.noreply.github.com>
2021-02-21 15:02:42 +00:00
|
|
|
"reflect"
|
2017-12-19 20:28:12 +00:00
|
|
|
"sync"
|
2017-02-01 20:35:40 +00:00
|
|
|
|
2021-02-16 20:31:50 +00:00
|
|
|
"github.com/v2fly/v2ray-core/v4/common"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/common/log"
|
2015-09-11 15:27:36 +00:00
|
|
|
)
|
|
|
|
|
2018-04-03 09:11:54 +00:00
|
|
|
// Instance is a log.Handler that handles logs.
|
2017-12-19 20:28:12 +00:00
|
|
|
type Instance struct {
|
|
|
|
sync.RWMutex
|
|
|
|
config *Config
|
2017-12-21 23:41:40 +00:00
|
|
|
accessLogger log.Handler
|
|
|
|
errorLogger log.Handler
|
improve commands (#648)
* stat show json refactor
> will show {} since scripts should expect it, its the json style of blank value
* combine statistics commands to one
* code optimize
* fix runtime flag
* remove json indent
* change overridden to override
* api bi -json
* convert stdin support
code optimize
* writeRow() code optimize
add stats tittle
revert back to restartlogger
* api log -restart
* follow log
* codeql
* move -json to shared flags
* flags optimize
* update flag descriptions
* change "-v" of "api bo" to duration
* change "-expire" of "tls cert" to days
* cmds short description optimize
* fix multiple log followers
* Format loader refactor
* "infra/conf/merge" refactor
* "LoadConfig" refactor
* add "infra/conf/mergers"
* contribute to it will benifit `v2ray run`,`v2ray test`,`v2ray convert`
* easily add new formats, by just adding a converter like json.FromTOML
* default format auto, to all cmds above
* auto detect input format
* mixed formats support
* better stdin behavior
* don't wait if no content
* don't use 'stdin:' placeholder
* `v2ray test` now behaves exactly the same with `v2ray run`, including stdin reading
* api ado, adi, rmo, rmi refactor
* support folders to files resolving, mixed formats
* remove remaining 'stdin:' placeholders
* fix tests
* os.Stdin.Stat() behaves different in platforms, removed
* code optimize
Co-authored-by: loyalsoldier <10487845+Loyalsoldier@users.noreply.github.com>
2021-02-21 15:02:42 +00:00
|
|
|
followers map[reflect.Value]func(msg log.Message)
|
2017-12-21 23:41:40 +00:00
|
|
|
active bool
|
2017-12-19 20:28:12 +00:00
|
|
|
}
|
2015-10-13 16:27:29 +00:00
|
|
|
|
2017-12-19 23:09:52 +00:00
|
|
|
// New creates a new log.Instance based on the given config.
|
2017-12-19 20:28:12 +00:00
|
|
|
func New(ctx context.Context, config *Config) (*Instance, error) {
|
2017-12-21 23:41:40 +00:00
|
|
|
g := &Instance{
|
2017-12-19 20:28:12 +00:00
|
|
|
config: config,
|
2018-02-14 21:00:08 +00:00
|
|
|
active: false,
|
2017-12-21 23:41:40 +00:00
|
|
|
}
|
2018-02-14 21:00:08 +00:00
|
|
|
log.RegisterHandler(g)
|
2017-12-21 23:41:40 +00:00
|
|
|
|
2019-11-13 08:31:09 +00:00
|
|
|
// start logger instantly on inited
|
|
|
|
// other modules would log during init
|
|
|
|
if err := g.startInternal(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
newError("Logger started").AtDebug().WriteToLog()
|
2017-12-21 23:41:40 +00:00
|
|
|
return g, nil
|
2017-12-19 20:28:12 +00:00
|
|
|
}
|
2015-10-13 16:27:29 +00:00
|
|
|
|
2017-12-19 20:28:12 +00:00
|
|
|
func (g *Instance) initAccessLogger() error {
|
2018-12-06 16:37:05 +00:00
|
|
|
handler, err := createHandler(g.config.AccessLogType, HandlerCreatorOptions{
|
|
|
|
Path: g.config.AccessLogPath,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-12-05 20:10:14 +00:00
|
|
|
}
|
2018-12-06 16:37:05 +00:00
|
|
|
g.accessLogger = handler
|
2015-12-05 20:10:14 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-12-19 20:28:12 +00:00
|
|
|
func (g *Instance) initErrorLogger() error {
|
2018-12-06 16:37:05 +00:00
|
|
|
handler, err := createHandler(g.config.ErrorLogType, HandlerCreatorOptions{
|
|
|
|
Path: g.config.ErrorLogPath,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2017-04-06 13:13:09 +00:00
|
|
|
}
|
2018-12-06 16:37:05 +00:00
|
|
|
g.errorLogger = handler
|
2017-12-19 20:28:12 +00:00
|
|
|
return nil
|
2017-04-06 13:13:09 +00:00
|
|
|
}
|
|
|
|
|
2018-02-14 21:00:08 +00:00
|
|
|
// Type implements common.HasType.
|
2018-02-14 16:35:09 +00:00
|
|
|
func (*Instance) Type() interface{} {
|
|
|
|
return (*Instance)(nil)
|
|
|
|
}
|
|
|
|
|
2018-02-14 21:00:08 +00:00
|
|
|
func (g *Instance) startInternal() error {
|
2017-12-21 23:41:40 +00:00
|
|
|
g.Lock()
|
|
|
|
defer g.Unlock()
|
2018-02-14 16:35:09 +00:00
|
|
|
|
|
|
|
if g.active {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-12-21 23:41:40 +00:00
|
|
|
g.active = true
|
2017-02-01 20:35:40 +00:00
|
|
|
|
2018-02-14 16:35:09 +00:00
|
|
|
if err := g.initAccessLogger(); err != nil {
|
|
|
|
return newError("failed to initialize access logger").Base(err).AtWarning()
|
|
|
|
}
|
|
|
|
if err := g.initErrorLogger(); err != nil {
|
|
|
|
return newError("failed to initialize error logger").Base(err).AtWarning()
|
|
|
|
}
|
2018-02-14 21:00:08 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-04-03 09:11:54 +00:00
|
|
|
// Start implements common.Runnable.Start().
|
2018-02-14 21:00:08 +00:00
|
|
|
func (g *Instance) Start() error {
|
2019-11-13 08:31:09 +00:00
|
|
|
return g.startInternal()
|
2017-12-21 23:41:40 +00:00
|
|
|
}
|
|
|
|
|
improve commands (#648)
* stat show json refactor
> will show {} since scripts should expect it, its the json style of blank value
* combine statistics commands to one
* code optimize
* fix runtime flag
* remove json indent
* change overridden to override
* api bi -json
* convert stdin support
code optimize
* writeRow() code optimize
add stats tittle
revert back to restartlogger
* api log -restart
* follow log
* codeql
* move -json to shared flags
* flags optimize
* update flag descriptions
* change "-v" of "api bo" to duration
* change "-expire" of "tls cert" to days
* cmds short description optimize
* fix multiple log followers
* Format loader refactor
* "infra/conf/merge" refactor
* "LoadConfig" refactor
* add "infra/conf/mergers"
* contribute to it will benifit `v2ray run`,`v2ray test`,`v2ray convert`
* easily add new formats, by just adding a converter like json.FromTOML
* default format auto, to all cmds above
* auto detect input format
* mixed formats support
* better stdin behavior
* don't wait if no content
* don't use 'stdin:' placeholder
* `v2ray test` now behaves exactly the same with `v2ray run`, including stdin reading
* api ado, adi, rmo, rmi refactor
* support folders to files resolving, mixed formats
* remove remaining 'stdin:' placeholders
* fix tests
* os.Stdin.Stat() behaves different in platforms, removed
* code optimize
Co-authored-by: loyalsoldier <10487845+Loyalsoldier@users.noreply.github.com>
2021-02-21 15:02:42 +00:00
|
|
|
// AddFollower implements log.Follower.
|
|
|
|
func (g *Instance) AddFollower(f func(msg log.Message)) {
|
|
|
|
g.Lock()
|
|
|
|
defer g.Unlock()
|
|
|
|
if g.followers == nil {
|
|
|
|
g.followers = make(map[reflect.Value]func(msg log.Message))
|
|
|
|
}
|
|
|
|
g.followers[reflect.ValueOf(f)] = f
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveFollower implements log.Follower.
|
|
|
|
func (g *Instance) RemoveFollower(f func(msg log.Message)) {
|
|
|
|
g.Lock()
|
|
|
|
defer g.Unlock()
|
|
|
|
delete(g.followers, reflect.ValueOf(f))
|
|
|
|
}
|
|
|
|
|
2017-12-19 23:09:52 +00:00
|
|
|
// Handle implements log.Handler.
|
2017-12-19 20:28:12 +00:00
|
|
|
func (g *Instance) Handle(msg log.Message) {
|
2018-02-14 16:35:09 +00:00
|
|
|
g.RLock()
|
|
|
|
defer g.RUnlock()
|
|
|
|
|
|
|
|
if !g.active {
|
2017-12-21 23:41:40 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
improve commands (#648)
* stat show json refactor
> will show {} since scripts should expect it, its the json style of blank value
* combine statistics commands to one
* code optimize
* fix runtime flag
* remove json indent
* change overridden to override
* api bi -json
* convert stdin support
code optimize
* writeRow() code optimize
add stats tittle
revert back to restartlogger
* api log -restart
* follow log
* codeql
* move -json to shared flags
* flags optimize
* update flag descriptions
* change "-v" of "api bo" to duration
* change "-expire" of "tls cert" to days
* cmds short description optimize
* fix multiple log followers
* Format loader refactor
* "infra/conf/merge" refactor
* "LoadConfig" refactor
* add "infra/conf/mergers"
* contribute to it will benifit `v2ray run`,`v2ray test`,`v2ray convert`
* easily add new formats, by just adding a converter like json.FromTOML
* default format auto, to all cmds above
* auto detect input format
* mixed formats support
* better stdin behavior
* don't wait if no content
* don't use 'stdin:' placeholder
* `v2ray test` now behaves exactly the same with `v2ray run`, including stdin reading
* api ado, adi, rmo, rmi refactor
* support folders to files resolving, mixed formats
* remove remaining 'stdin:' placeholders
* fix tests
* os.Stdin.Stat() behaves different in platforms, removed
* code optimize
Co-authored-by: loyalsoldier <10487845+Loyalsoldier@users.noreply.github.com>
2021-02-21 15:02:42 +00:00
|
|
|
for _, f := range g.followers {
|
|
|
|
f(msg)
|
|
|
|
}
|
|
|
|
|
2017-12-19 20:28:12 +00:00
|
|
|
switch msg := msg.(type) {
|
|
|
|
case *log.AccessMessage:
|
|
|
|
if g.accessLogger != nil {
|
2017-12-21 23:41:40 +00:00
|
|
|
g.accessLogger.Handle(msg)
|
2017-02-01 20:35:40 +00:00
|
|
|
}
|
2017-12-19 20:28:12 +00:00
|
|
|
case *log.GeneralMessage:
|
2017-12-21 23:41:40 +00:00
|
|
|
if g.errorLogger != nil && msg.Severity <= g.config.ErrorLogLevel {
|
|
|
|
g.errorLogger.Handle(msg)
|
2017-02-01 20:35:40 +00:00
|
|
|
}
|
2017-12-19 20:28:12 +00:00
|
|
|
default:
|
|
|
|
// Swallow
|
2017-02-01 20:35:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-03 09:11:54 +00:00
|
|
|
// Close implements common.Closable.Close().
|
2018-02-08 14:39:46 +00:00
|
|
|
func (g *Instance) Close() error {
|
2018-02-14 16:35:09 +00:00
|
|
|
newError("Logger closing").AtDebug().WriteToLog()
|
|
|
|
|
2017-12-19 20:28:12 +00:00
|
|
|
g.Lock()
|
|
|
|
defer g.Unlock()
|
|
|
|
|
2018-02-14 16:35:09 +00:00
|
|
|
if !g.active {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-12-21 23:42:27 +00:00
|
|
|
g.active = false
|
2018-02-08 14:39:46 +00:00
|
|
|
|
2020-10-11 11:22:46 +00:00
|
|
|
common.Close(g.accessLogger)
|
2018-02-14 21:00:08 +00:00
|
|
|
g.accessLogger = nil
|
|
|
|
|
2020-10-11 11:22:46 +00:00
|
|
|
common.Close(g.errorLogger)
|
2018-02-14 21:00:08 +00:00
|
|
|
g.errorLogger = nil
|
2018-02-14 16:35:09 +00:00
|
|
|
|
2018-02-08 14:39:46 +00:00
|
|
|
return nil
|
2016-07-10 21:11:42 +00:00
|
|
|
}
|
2017-02-01 20:35:40 +00:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
|
|
|
|
return New(ctx, config.(*Config))
|
|
|
|
}))
|
|
|
|
}
|