mirror of
https://gitee.com/johng/gf.git
synced 2024-11-30 03:07:45 +08:00
comment update for package glog
This commit is contained in:
parent
fc1dfb7ff9
commit
f1857df5e2
@ -109,8 +109,10 @@ func (entry *Entry) Close() {
|
||||
// gcron.Entry relies on gtimer to implement a scheduled task check for gcron.Entry per second.
|
||||
func (entry *Entry) check() {
|
||||
if entry.schedule.meet(time.Now()) {
|
||||
path := entry.cron.GetLogPath()
|
||||
level := entry.cron.GetLogLevel()
|
||||
var (
|
||||
path = entry.cron.GetLogPath()
|
||||
level = entry.cron.GetLogLevel()
|
||||
)
|
||||
switch entry.cron.status.Val() {
|
||||
case StatusStopped:
|
||||
return
|
||||
@ -122,6 +124,23 @@ func (entry *Entry) check() {
|
||||
case StatusReady:
|
||||
fallthrough
|
||||
case StatusRunning:
|
||||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
glog.Path(path).Level(level).Errorf(
|
||||
"[gcron] %s(%s) %s end with error: %+v",
|
||||
entry.Name, entry.schedule.pattern, entry.jobName, err,
|
||||
)
|
||||
} else {
|
||||
glog.Path(path).Level(level).Debugf(
|
||||
"[gcron] %s(%s) %s end",
|
||||
entry.Name, entry.schedule.pattern, entry.jobName,
|
||||
)
|
||||
}
|
||||
if entry.entry.Status() == StatusClosed {
|
||||
entry.Close()
|
||||
}
|
||||
}()
|
||||
|
||||
// Running times check.
|
||||
times := entry.times.Add(-1)
|
||||
if times <= 0 {
|
||||
@ -133,16 +152,7 @@ func (entry *Entry) check() {
|
||||
entry.times.Set(defaultTimes)
|
||||
}
|
||||
glog.Path(path).Level(level).Debugf("[gcron] %s(%s) %s start", entry.Name, entry.schedule.pattern, entry.jobName)
|
||||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
glog.Path(path).Level(level).Errorf("[gcron] %s(%s) %s end with error: %v", entry.Name, entry.schedule.pattern, entry.jobName, err)
|
||||
} else {
|
||||
glog.Path(path).Level(level).Debugf("[gcron] %s(%s) %s end", entry.Name, entry.schedule.pattern, entry.jobName)
|
||||
}
|
||||
if entry.entry.Status() == StatusClosed {
|
||||
entry.Close()
|
||||
}
|
||||
}()
|
||||
|
||||
entry.Job()
|
||||
|
||||
}
|
||||
|
@ -31,7 +31,7 @@ type cronSchedule struct {
|
||||
|
||||
const (
|
||||
// regular expression for cron pattern, which contains 6 parts of time units.
|
||||
gREGEX_FOR_CRON = `^([\-/\d\*\?,]+)\s+([\-/\d\*\?,]+)\s+([\-/\d\*\?,]+)\s+([\-/\d\*\?,]+)\s+([\-/\d\*\?,A-Za-z]+)\s+([\-/\d\*\?,A-Za-z]+)$`
|
||||
regexForCron = `^([\-/\d\*\?,]+)\s+([\-/\d\*\?,]+)\s+([\-/\d\*\?,]+)\s+([\-/\d\*\?,]+)\s+([\-/\d\*\?,A-Za-z]+)\s+([\-/\d\*\?,A-Za-z]+)$`
|
||||
)
|
||||
|
||||
var (
|
||||
@ -95,7 +95,7 @@ func newSchedule(pattern string) (*cronSchedule, error) {
|
||||
}
|
||||
// Handle the common cron pattern, like:
|
||||
// 0 0 0 1 1 2
|
||||
if match, _ := gregex.MatchString(gREGEX_FOR_CRON, pattern); len(match) == 7 {
|
||||
if match, _ := gregex.MatchString(regexForCron, pattern); len(match) == 7 {
|
||||
schedule := &cronSchedule{
|
||||
create: time.Now().Unix(),
|
||||
every: 0,
|
||||
|
@ -6,14 +6,14 @@
|
||||
|
||||
package glog
|
||||
|
||||
// Print prints <v> with newline using fmt.Sprintln.
|
||||
// The parameter <v> can be multiple variables.
|
||||
// Print prints `v` with newline using fmt.Sprintln.
|
||||
// The parameter `v` can be multiple variables.
|
||||
func Print(v ...interface{}) {
|
||||
logger.Print(v...)
|
||||
}
|
||||
|
||||
// Printf prints <v> with format <format> using fmt.Sprintf.
|
||||
// The parameter <v> can be multiple variables.
|
||||
// Printf prints `v` with format `format` using fmt.Sprintf.
|
||||
// The parameter `v` can be multiple variables.
|
||||
func Printf(format string, v ...interface{}) {
|
||||
logger.Printf(format, v...)
|
||||
}
|
||||
|
@ -18,31 +18,31 @@ func Expose() *Logger {
|
||||
|
||||
// Ctx is a chaining function,
|
||||
// which sets the context for current logging.
|
||||
// The parameter <keys> specifies the context keys for retrieving values.
|
||||
// The parameter `keys` specifies the context keys for retrieving values.
|
||||
func Ctx(ctx context.Context, keys ...interface{}) *Logger {
|
||||
return logger.Ctx(ctx, keys...)
|
||||
}
|
||||
|
||||
// To is a chaining function,
|
||||
// which redirects current logging content output to the sepecified <writer>.
|
||||
// which redirects current logging content output to the sepecified `writer`.
|
||||
func To(writer io.Writer) *Logger {
|
||||
return logger.To(writer)
|
||||
}
|
||||
|
||||
// Path is a chaining function,
|
||||
// which sets the directory path to <path> for current logging content output.
|
||||
// which sets the directory path to `path` for current logging content output.
|
||||
func Path(path string) *Logger {
|
||||
return logger.Path(path)
|
||||
}
|
||||
|
||||
// Cat is a chaining function,
|
||||
// which sets the category to <category> for current logging content output.
|
||||
// which sets the category to `category` for current logging content output.
|
||||
func Cat(category string) *Logger {
|
||||
return logger.Cat(category)
|
||||
}
|
||||
|
||||
// File is a chaining function,
|
||||
// which sets file name <pattern> for the current logging content output.
|
||||
// which sets file name `pattern` for the current logging content output.
|
||||
func File(pattern string) *Logger {
|
||||
return logger.File(pattern)
|
||||
}
|
||||
@ -94,7 +94,7 @@ func Header(enabled ...bool) *Logger {
|
||||
|
||||
// Line is a chaining function,
|
||||
// which enables/disables printing its caller file along with its line number.
|
||||
// The parameter <long> specified whether print the long absolute file path, eg: /a/b/c/d.go:23.
|
||||
// The parameter `long` specified whether print the long absolute file path, eg: /a/b/c/d.go:23.
|
||||
func Line(long ...bool) *Logger {
|
||||
return logger.Line(long...)
|
||||
}
|
||||
|
@ -31,8 +31,8 @@ func GetPath() string {
|
||||
return logger.GetPath()
|
||||
}
|
||||
|
||||
// SetFile sets the file name <pattern> for file logging.
|
||||
// Datetime pattern can be used in <pattern>, eg: access-{Ymd}.log.
|
||||
// SetFile sets the file name `pattern` for file logging.
|
||||
// Datetime pattern can be used in `pattern`, eg: access-{Ymd}.log.
|
||||
// The default file name pattern is: Y-m-d.log, eg: 2018-01-01.log
|
||||
func SetFile(pattern string) {
|
||||
logger.SetFile(pattern)
|
||||
@ -48,9 +48,9 @@ func GetLevel() int {
|
||||
return logger.GetLevel()
|
||||
}
|
||||
|
||||
// SetWriter sets the customized logging <writer> for logging.
|
||||
// The <writer> object should implements the io.Writer interface.
|
||||
// Developer can use customized logging <writer> to redirect logging output to another service,
|
||||
// SetWriter sets the customized logging `writer` for logging.
|
||||
// The `writer` object should implements the io.Writer interface.
|
||||
// Developer can use customized logging `writer` to redirect logging output to another service,
|
||||
// eg: kafka, mysql, mongodb, etc.
|
||||
func SetWriter(writer io.Writer) {
|
||||
logger.SetWriter(writer)
|
||||
@ -113,13 +113,13 @@ func GetCtxKeys() []interface{} {
|
||||
}
|
||||
|
||||
// PrintStack prints the caller stack,
|
||||
// the optional parameter <skip> specify the skipped stack offset from the end point.
|
||||
// the optional parameter `skip` specify the skipped stack offset from the end point.
|
||||
func PrintStack(skip ...int) {
|
||||
logger.PrintStack(skip...)
|
||||
}
|
||||
|
||||
// GetStack returns the caller stack content,
|
||||
// the optional parameter <skip> specify the skipped stack offset from the end point.
|
||||
// the optional parameter `skip` specify the skipped stack offset from the end point.
|
||||
func GetStack(skip ...int) string {
|
||||
return logger.GetStack(skip...)
|
||||
}
|
||||
|
@ -19,7 +19,7 @@ var (
|
||||
)
|
||||
|
||||
// Instance returns an instance of Logger with default settings.
|
||||
// The parameter <name> is the name for the instance.
|
||||
// The parameter `name` is the name for the instance.
|
||||
func Instance(name ...string) *Logger {
|
||||
key := DefaultName
|
||||
if len(name) > 0 && name[0] != "" {
|
||||
|
@ -93,7 +93,7 @@ func (l *Logger) getFilePath(now time.Time) string {
|
||||
return file
|
||||
}
|
||||
|
||||
// print prints <s> to defined writer, logging file or passed <std>.
|
||||
// print prints `s` to defined writer, logging file or passed `std`.
|
||||
func (l *Logger) print(ctx context.Context, level int, values ...interface{}) {
|
||||
// Lazy initialize for rotation feature.
|
||||
// It uses atomic reading operation to enhance the performance checking.
|
||||
@ -309,12 +309,12 @@ func (l *Logger) getCtx() context.Context {
|
||||
return context.TODO()
|
||||
}
|
||||
|
||||
// printStd prints content <s> without stack.
|
||||
// printStd prints content `s` without stack.
|
||||
func (l *Logger) printStd(level int, value ...interface{}) {
|
||||
l.print(l.getCtx(), level, value...)
|
||||
}
|
||||
|
||||
// printStd prints content <s> with stack check.
|
||||
// printStd prints content `s` with stack check.
|
||||
func (l *Logger) printErr(level int, value ...interface{}) {
|
||||
if l.config.StStatus == 1 {
|
||||
if s := l.GetStack(); s != "" {
|
||||
@ -325,13 +325,13 @@ func (l *Logger) printErr(level int, value ...interface{}) {
|
||||
l.print(l.getCtx(), level, value...)
|
||||
}
|
||||
|
||||
// format formats <values> using fmt.Sprintf.
|
||||
// format formats `values` using fmt.Sprintf.
|
||||
func (l *Logger) format(format string, value ...interface{}) string {
|
||||
return fmt.Sprintf(format, value...)
|
||||
}
|
||||
|
||||
// PrintStack prints the caller stack,
|
||||
// the optional parameter <skip> specify the skipped stack offset from the end point.
|
||||
// the optional parameter `skip` specify the skipped stack offset from the end point.
|
||||
func (l *Logger) PrintStack(skip ...int) {
|
||||
if s := l.GetStack(skip...); s != "" {
|
||||
l.Println("Stack:\n" + s)
|
||||
@ -341,7 +341,7 @@ func (l *Logger) PrintStack(skip ...int) {
|
||||
}
|
||||
|
||||
// GetStack returns the caller stack content,
|
||||
// the optional parameter <skip> specify the skipped stack offset from the end point.
|
||||
// the optional parameter `skip` specify the skipped stack offset from the end point.
|
||||
func (l *Logger) GetStack(skip ...int) string {
|
||||
stackSkip := l.config.StSkip
|
||||
if len(skip) > 0 {
|
||||
|
@ -11,14 +11,14 @@ import (
|
||||
"os"
|
||||
)
|
||||
|
||||
// Print prints <v> with newline using fmt.Sprintln.
|
||||
// The parameter <v> can be multiple variables.
|
||||
// Print prints `v` with newline using fmt.Sprintln.
|
||||
// The parameter `v` can be multiple variables.
|
||||
func (l *Logger) Print(v ...interface{}) {
|
||||
l.printStd(LEVEL_NONE, v...)
|
||||
}
|
||||
|
||||
// Printf prints <v> with format <format> using fmt.Sprintf.
|
||||
// The parameter <v> can be multiple variables.
|
||||
// Printf prints `v` with format `format` using fmt.Sprintf.
|
||||
// The parameter `v` can be multiple variables.
|
||||
func (l *Logger) Printf(format string, v ...interface{}) {
|
||||
l.printStd(LEVEL_NONE, l.format(format, v...))
|
||||
}
|
||||
@ -145,7 +145,7 @@ func (l *Logger) Criticalf(format string, v ...interface{}) {
|
||||
}
|
||||
}
|
||||
|
||||
// checkLevel checks whether the given <level> could be output.
|
||||
// checkLevel checks whether the given `level` could be output.
|
||||
func (l *Logger) checkLevel(level int) bool {
|
||||
return l.config.Level&level > 0
|
||||
}
|
||||
|
@ -34,7 +34,7 @@ func (l *Logger) Ctx(ctx context.Context, keys ...interface{}) *Logger {
|
||||
}
|
||||
|
||||
// To is a chaining function,
|
||||
// which redirects current logging content output to the specified <writer>.
|
||||
// which redirects current logging content output to the specified `writer`.
|
||||
func (l *Logger) To(writer io.Writer) *Logger {
|
||||
logger := (*Logger)(nil)
|
||||
if l.parent == nil {
|
||||
@ -47,9 +47,9 @@ func (l *Logger) To(writer io.Writer) *Logger {
|
||||
}
|
||||
|
||||
// Path is a chaining function,
|
||||
// which sets the directory path to <path> for current logging content output.
|
||||
// which sets the directory path to `path` for current logging content output.
|
||||
//
|
||||
// Note that the parameter <path> is a directory path, not a file path.
|
||||
// Note that the parameter `path` is a directory path, not a file path.
|
||||
func (l *Logger) Path(path string) *Logger {
|
||||
logger := (*Logger)(nil)
|
||||
if l.parent == nil {
|
||||
@ -67,8 +67,8 @@ func (l *Logger) Path(path string) *Logger {
|
||||
}
|
||||
|
||||
// Cat is a chaining function,
|
||||
// which sets the category to <category> for current logging content output.
|
||||
// Param <category> can be hierarchical, eg: module/user.
|
||||
// which sets the category to `category` for current logging content output.
|
||||
// Param `category` can be hierarchical, eg: module/user.
|
||||
func (l *Logger) Cat(category string) *Logger {
|
||||
logger := (*Logger)(nil)
|
||||
if l.parent == nil {
|
||||
@ -86,7 +86,7 @@ func (l *Logger) Cat(category string) *Logger {
|
||||
}
|
||||
|
||||
// File is a chaining function,
|
||||
// which sets file name <pattern> for the current logging content output.
|
||||
// which sets file name `pattern` for the current logging content output.
|
||||
func (l *Logger) File(file string) *Logger {
|
||||
logger := (*Logger)(nil)
|
||||
if l.parent == nil {
|
||||
@ -181,7 +181,7 @@ func (l *Logger) Stdout(enabled ...bool) *Logger {
|
||||
} else {
|
||||
logger = l
|
||||
}
|
||||
// stdout printing is enabled if <enabled> is not passed.
|
||||
// stdout printing is enabled if `enabled` is not passed.
|
||||
if len(enabled) > 0 && !enabled[0] {
|
||||
logger.config.StdoutPrint = false
|
||||
} else {
|
||||
@ -200,7 +200,7 @@ func (l *Logger) Header(enabled ...bool) *Logger {
|
||||
} else {
|
||||
logger = l
|
||||
}
|
||||
// header is enabled if <enabled> is not passed.
|
||||
// header is enabled if `enabled` is not passed.
|
||||
if len(enabled) > 0 && !enabled[0] {
|
||||
logger.SetHeaderPrint(false)
|
||||
} else {
|
||||
@ -211,7 +211,7 @@ func (l *Logger) Header(enabled ...bool) *Logger {
|
||||
|
||||
// Line is a chaining function,
|
||||
// which enables/disables printing its caller file path along with its line number.
|
||||
// The parameter <long> specified whether print the long absolute file path, eg: /a/b/c/d.go:23,
|
||||
// The parameter `long` specified whether print the long absolute file path, eg: /a/b/c/d.go:23,
|
||||
// or else short one: d.go:23.
|
||||
func (l *Logger) Line(long ...bool) *Logger {
|
||||
logger := (*Logger)(nil)
|
||||
@ -237,7 +237,7 @@ func (l *Logger) Async(enabled ...bool) *Logger {
|
||||
} else {
|
||||
logger = l
|
||||
}
|
||||
// async feature is enabled if <enabled> is not passed.
|
||||
// async feature is enabled if `enabled` is not passed.
|
||||
if len(enabled) > 0 && !enabled[0] {
|
||||
logger.SetAsync(false)
|
||||
} else {
|
||||
|
@ -188,9 +188,9 @@ func (l *Logger) GetCtxKeys() []interface{} {
|
||||
return l.config.CtxKeys
|
||||
}
|
||||
|
||||
// SetWriter sets the customized logging <writer> for logging.
|
||||
// The <writer> object should implements the io.Writer interface.
|
||||
// Developer can use customized logging <writer> to redirect logging output to another service,
|
||||
// SetWriter sets the customized logging `writer` for logging.
|
||||
// The `writer` object should implements the io.Writer interface.
|
||||
// Developer can use customized logging `writer` to redirect logging output to another service,
|
||||
// eg: kafka, mysql, mongodb, etc.
|
||||
func (l *Logger) SetWriter(writer io.Writer) {
|
||||
l.config.Writer = writer
|
||||
@ -222,8 +222,8 @@ func (l *Logger) GetPath() string {
|
||||
return l.config.Path
|
||||
}
|
||||
|
||||
// SetFile sets the file name <pattern> for file logging.
|
||||
// Datetime pattern can be used in <pattern>, eg: access-{Ymd}.log.
|
||||
// SetFile sets the file name `pattern` for file logging.
|
||||
// Datetime pattern can be used in `pattern`, eg: access-{Ymd}.log.
|
||||
// The default file name pattern is: Y-m-d.log, eg: 2018-01-01.log
|
||||
func (l *Logger) SetFile(pattern string) {
|
||||
l.config.File = pattern
|
||||
|
Loading…
Reference in New Issue
Block a user