add error stack for internal error printing

This commit is contained in:
John Guo
2022-01-28 14:51:49 +08:00
parent 1a000396e2
commit 04eb654133
55 changed files with 175 additions and 240 deletions

View File

@ -28,7 +28,7 @@ func init() {
if builtInVarStr != "" {
err := json.UnmarshalUseNumber(gbase64.MustDecodeString(builtInVarStr), &builtInVarMap)
if err != nil {
intlog.Error(context.TODO(), err)
intlog.Errorf(context.TODO(), `%+v`, err)
}
builtInVarMap["gfVersion"] = gf.VERSION
builtInVarMap["goVersion"] = runtime.Version()

View File

@ -64,7 +64,7 @@ func Instance(name ...string) *Config {
return localInstances.GetOrSetFuncLock(key, func() interface{} {
adapter, err := NewAdapterFile()
if err != nil {
intlog.Error(context.Background(), err)
intlog.Errorf(context.Background(), `%+v`, err)
return nil
}
// If it's not using default configuration or its configuration file is not available,

View File

@ -87,20 +87,20 @@ func NewAdapterFile(file ...string) (*AdapterFile, error) {
// Dir path of working dir.
if err = config.AddPath(gfile.Pwd()); err != nil {
intlog.Error(context.TODO(), err)
intlog.Errorf(context.TODO(), `%+v`, err)
}
// Dir path of main package.
if mainPath := gfile.MainPkgPath(); mainPath != "" && gfile.Exists(mainPath) {
if err = config.AddPath(mainPath); err != nil {
intlog.Error(context.TODO(), err)
intlog.Errorf(context.TODO(), `%+v`, err)
}
}
// Dir path of binary.
if selfPath := gfile.SelfDir(); selfPath != "" && gfile.Exists(selfPath) {
if err = config.AddPath(selfPath); err != nil {
intlog.Error(context.TODO(), err)
intlog.Errorf(context.TODO(), `%+v`, err)
}
}
}

View File

@ -65,7 +65,7 @@ func GetBytesWithCache(path string, duration ...time.Duration) []byte {
_, _ = gfsnotify.Add(path, func(event *gfsnotify.Event) {
_, err := internalCache.Remove(ctx, cacheKey)
if err != nil {
intlog.Error(ctx, err)
intlog.Errorf(ctx, `%+v`, err)
}
gfsnotify.Exit()
})

View File

@ -108,7 +108,7 @@ func (w *Watcher) addWithCallbackFunc(name, path string, callbackFunc func(event
func (w *Watcher) Close() {
w.events.Close()
if err := w.watcher.Close(); err != nil {
intlog.Error(context.TODO(), err)
intlog.Errorf(context.TODO(), `%+v`, err)
}
close(w.closeChan)
}
@ -131,7 +131,7 @@ func (w *Watcher) Remove(path string) error {
for _, subPath := range subPaths {
if w.checkPathCanBeRemoved(subPath) {
if internalErr := w.watcher.Remove(subPath); internalErr != nil {
intlog.Error(context.TODO(), internalErr)
intlog.Errorf(context.TODO(), `%+v`, internalErr)
}
}
}

View File

@ -39,11 +39,11 @@ func (w *Watcher) watchLoop() {
}, repeatEventFilterDuration,
)
if err != nil {
intlog.Error(context.TODO(), err)
intlog.Errorf(context.TODO(), `%+v`, err)
}
case err := <-w.watcher.Errors:
intlog.Error(context.TODO(), err)
intlog.Errorf(context.TODO(), `%+v`, err)
}
}
}()
@ -69,7 +69,7 @@ func (w *Watcher) eventLoop() {
// It adds the path back to monitor.
// We need no worry about the repeat adding.
if err := w.watcher.Add(event.Path); err != nil {
intlog.Error(context.TODO(), err)
intlog.Errorf(context.TODO(), `%+v`, err)
} else {
intlog.Printf(context.TODO(), "fake remove event, watcher re-adds monitor for: %s", event.Path)
}
@ -85,7 +85,7 @@ func (w *Watcher) eventLoop() {
// It might lost the monitoring for the path, so we add the path back to monitor.
// We need no worry about the repeat adding.
if err := w.watcher.Add(event.Path); err != nil {
intlog.Error(context.TODO(), err)
intlog.Errorf(context.TODO(), `%+v`, err)
} else {
intlog.Printf(context.TODO(), "fake rename event, watcher re-adds monitor for: %s", event.Path)
}
@ -103,7 +103,7 @@ func (w *Watcher) eventLoop() {
for _, subPath := range fileAllDirs(event.Path) {
if fileIsDir(subPath) {
if err := w.watcher.Add(subPath); err != nil {
intlog.Error(context.TODO(), err)
intlog.Errorf(context.TODO(), `%+v`, err)
} else {
intlog.Printf(context.TODO(), "folder creation event, watcher adds monitor for: %s", subPath)
}
@ -112,7 +112,7 @@ func (w *Watcher) eventLoop() {
} else {
// If it's a file, it directly adds it to monitor.
if err := w.watcher.Add(event.Path); err != nil {
intlog.Error(context.TODO(), err)
intlog.Errorf(context.TODO(), `%+v`, err)
} else {
intlog.Printf(context.TODO(), "file creation event, watcher adds monitor for: %s", event.Path)
}

View File

@ -224,7 +224,7 @@ func (l *Logger) print(ctx context.Context, level int, values ...interface{}) {
input.Next()
})
if err != nil {
intlog.Error(ctx, err)
intlog.Errorf(ctx, `%+v`, err)
}
} else {
input.Next()
@ -264,7 +264,7 @@ func (l *Logger) printToWriter(ctx context.Context, input *HandlerInput) *bytes.
buffer = input.getRealBuffer(l.config.WriterColorEnable)
)
if _, err := l.config.Writer.Write(buffer.Bytes()); err != nil {
intlog.Error(ctx, err)
intlog.Errorf(ctx, `%+v`, err)
}
return buffer
}
@ -281,7 +281,7 @@ func (l *Logger) printToStdout(ctx context.Context, input *HandlerInput) *bytes.
if l.config.StdoutColorDisabled {
// Output to stdout without color.
if _, err = os.Stdout.Write(buffer.Bytes()); err != nil {
intlog.Error(ctx, err)
intlog.Errorf(ctx, `%+v`, err)
}
} else {
// This will lose color in Windows os system.
@ -289,7 +289,7 @@ func (l *Logger) printToStdout(ctx context.Context, input *HandlerInput) *bytes.
// This will print color in Windows os system.
if _, err = fmt.Fprint(color.Output, buffer.String()); err != nil {
intlog.Error(ctx, err)
intlog.Errorf(ctx, `%+v`, err)
}
}
return buffer
@ -318,10 +318,10 @@ func (l *Logger) printToFile(ctx context.Context, t time.Time, in *HandlerInput)
intlog.Errorf(ctx, `got nil file pointer for: %s`, logFilePath)
} else {
if _, err := file.Write(buffer.Bytes()); err != nil {
intlog.Error(ctx, err)
intlog.Errorf(ctx, `%+v`, err)
}
if err := file.Close(); err != nil {
intlog.Error(ctx, err)
intlog.Errorf(ctx, `%+v`, err)
}
}
return buffer
@ -337,7 +337,7 @@ func (l *Logger) getFilePointer(ctx context.Context, path string) *gfpool.File {
)
if err != nil {
// panic(err)
intlog.Error(ctx, err)
intlog.Errorf(ctx, `%+v`, err)
}
return file
}

View File

@ -74,7 +74,7 @@ func (l *Logger) SetConfig(config Config) error {
// Necessary validation.
if config.Path != "" {
if err := l.SetPath(config.Path); err != nil {
intlog.Error(context.TODO(), err)
intlog.Errorf(context.TODO(), `%+v`, err)
return err
}
}

View File

@ -33,7 +33,7 @@ func (l *Logger) rotateFileBySize(ctx context.Context, now time.Time) {
}
if err := l.doRotateFile(ctx, l.getFilePath(now)); err != nil {
// panic(err)
intlog.Error(ctx, err)
intlog.Errorf(ctx, `%+v`, err)
}
}
@ -133,7 +133,7 @@ func (l *Logger) rotateChecksTimely(ctx context.Context) {
files, err = gfile.ScanDirFile(l.config.Path, pattern, true)
)
if err != nil {
intlog.Error(ctx, err)
intlog.Errorf(ctx, `%+v`, err)
}
intlog.Printf(ctx, "logging rotation start checks: %+v", files)
// =============================================================
@ -159,7 +159,7 @@ func (l *Logger) rotateChecksTimely(ctx context.Context) {
now, mtime, subDuration, l.config.RotateExpire, file,
)
if err := l.doRotateFile(ctx, file); err != nil {
intlog.Error(ctx, err)
intlog.Errorf(ctx, `%+v`, err)
}
}
}
@ -167,7 +167,7 @@ func (l *Logger) rotateChecksTimely(ctx context.Context) {
// Update the files array.
files, err = gfile.ScanDirFile(l.config.Path, pattern, true)
if err != nil {
intlog.Error(ctx, err)
intlog.Errorf(ctx, `%+v`, err)
}
}
}
@ -204,7 +204,7 @@ func (l *Logger) rotateChecksTimely(ctx context.Context) {
// Update the files array.
files, err = gfile.ScanDirFile(l.config.Path, pattern, true)
if err != nil {
intlog.Error(ctx, err)
intlog.Errorf(ctx, `%+v`, err)
}
}
}
@ -246,7 +246,7 @@ func (l *Logger) rotateChecksTimely(ctx context.Context) {
path, _ := array.PopLeft()
intlog.Printf(ctx, `remove exceeded backup limit file: %s`, path)
if err := gfile.Remove(path.(string)); err != nil {
intlog.Error(ctx, err)
intlog.Errorf(ctx, `%+v`, err)
}
}
}
@ -268,7 +268,7 @@ func (l *Logger) rotateChecksTimely(ctx context.Context) {
now, mtime, subDuration, l.config.RotateBackupExpire, path,
)
if err := gfile.Remove(path); err != nil {
intlog.Error(ctx, err)
intlog.Errorf(ctx, `%+v`, err)
}
return true
} else {

View File

@ -123,12 +123,12 @@ func (p *Process) Kill() (err error) {
}
if runtime.GOOS != "windows" {
if err = p.Process.Release(); err != nil {
intlog.Error(context.TODO(), err)
intlog.Errorf(context.TODO(), `%+v`, err)
}
}
// It ignores this error, just log it.
_, err = p.Process.Wait()
intlog.Error(context.TODO(), err)
intlog.Errorf(context.TODO(), `%+v`, err)
return nil
}

View File

@ -80,6 +80,6 @@ func (m *Manager) UpdateSessionTTL(sessionId string, data *gmap.StrAnyMap) {
ctx := context.Background()
err := m.sessionData.Set(ctx, sessionId, data, m.ttl)
if err != nil {
intlog.Error(ctx, err)
intlog.Errorf(ctx, `%+v`, err)
}
}

View File

@ -53,7 +53,7 @@ func (s *Session) init() error {
// Retrieve stored session data from storage.
if s.manager.storage != nil {
if s.data, err = s.manager.storage.GetSession(s.ctx, s.id, s.manager.ttl, s.data); err != nil && err != ErrorDisabled {
intlog.Errorf(s.ctx, "session restoring failed for id '%s': %v", s.id, err)
intlog.Errorf(s.ctx, `session restoring failed for id "%s": %+v`, s.id, err)
return err
}
}
@ -67,7 +67,7 @@ func (s *Session) init() error {
// Use default session id creating function of storage.
s.id, err = s.manager.storage.New(s.ctx, s.manager.ttl)
if err != nil && err != ErrorDisabled {
intlog.Errorf(s.ctx, "create session id failed: %v", err)
intlog.Errorf(s.ctx, "create session id failed: %+v", err)
return err
}
// If session storage does not implements id generating functionality,
@ -221,7 +221,7 @@ func (s *Session) Data() (map[string]interface{}, error) {
}
data, err := s.manager.storage.Data(s.ctx, s.id)
if err != nil && err != ErrorDisabled {
intlog.Error(s.ctx, err)
intlog.Errorf(s.ctx, `%+v`, err)
}
if data != nil {
return data, nil
@ -239,7 +239,7 @@ func (s *Session) Size() (int, error) {
}
size, err := s.manager.storage.GetSize(s.ctx, s.id)
if err != nil && err != ErrorDisabled {
intlog.Error(s.ctx, err)
intlog.Errorf(s.ctx, `%+v`, err)
}
if size >= 0 {
return size, nil
@ -279,7 +279,7 @@ func (s *Session) Get(key string, def ...interface{}) (*gvar.Var, error) {
}
v, err := s.manager.storage.Get(s.ctx, s.id, key)
if err != nil && err != ErrorDisabled {
intlog.Error(s.ctx, err)
intlog.Errorf(s.ctx, `%+v`, err)
return nil, err
}
if v != nil {

View File

@ -82,7 +82,7 @@ func (s *StorageFile) updateSessionTimely(ctx context.Context) {
break
}
if err = s.updateSessionTTl(context.TODO(), id); err != nil {
intlog.Error(context.TODO(), err)
intlog.Errorf(context.TODO(), `%+v`, err)
}
}
}

View File

@ -56,7 +56,7 @@ func NewStorageRedis(redis *gredis.Redis, prefix ...string) *StorageRedis {
break
} else {
if err = s.doUpdateTTL(context.TODO(), id, ttlSeconds); err != nil {
intlog.Error(context.TODO(), err)
intlog.Errorf(context.TODO(), `%+v`, err)
}
}
}

View File

@ -393,7 +393,7 @@ func ParseTimeFromContent(content string, format ...string) *Time {
if len(format) > 0 {
match, err = gregex.MatchString(formatToRegexPattern(format[0]), content)
if err != nil {
intlog.Error(context.TODO(), err)
intlog.Errorf(context.TODO(), `%+v`, err)
}
if len(match) > 0 {
return NewFromStrFormat(match[0], format[0])

View File

@ -75,14 +75,14 @@ func New(path ...string) *View {
}
if len(path) > 0 && len(path[0]) > 0 {
if err := view.SetPath(path[0]); err != nil {
intlog.Error(context.TODO(), err)
intlog.Errorf(context.TODO(), `%+v`, err)
}
} else {
// Customized dir path from env/cmd.
if envPath := gcmd.GetOptWithEnv(commandEnvKeyForPath).String(); envPath != "" {
if gfile.Exists(envPath) {
if err := view.SetPath(envPath); err != nil {
intlog.Error(context.TODO(), err)
intlog.Errorf(context.TODO(), `%+v`, err)
}
} else {
if errorPrint() {
@ -92,18 +92,18 @@ func New(path ...string) *View {
} else {
// Dir path of working dir.
if err := view.SetPath(gfile.Pwd()); err != nil {
intlog.Error(context.TODO(), err)
intlog.Errorf(context.TODO(), `%+v`, err)
}
// Dir path of binary.
if selfPath := gfile.SelfDir(); selfPath != "" && gfile.Exists(selfPath) {
if err := view.AddPath(selfPath); err != nil {
intlog.Error(context.TODO(), err)
intlog.Errorf(context.TODO(), `%+v`, err)
}
}
// Dir path of main package.
if mainPath := gfile.MainPkgPath(); mainPath != "" && gfile.Exists(mainPath) {
if err := view.AddPath(mainPath); err != nil {
intlog.Error(context.TODO(), err)
intlog.Errorf(context.TODO(), `%+v`, err)
}
}
}

View File

@ -86,7 +86,7 @@ func (view *View) Parse(ctx context.Context, file string, params ...Params) (res
templates.Clear()
gfsnotify.Exit()
}); err != nil {
intlog.Error(ctx, err)
intlog.Errorf(ctx, `%+v`, err)
}
}
return &fileCacheItem{