mirror of
https://github.com/anyproto/anytype-heart.git
synced 2025-06-11 18:20:33 +09:00
Merge pull request #1932 from anyproto/go-4689-save-logs-to-files
GO-4689 limit logs
This commit is contained in:
commit
eb7cb09169
1 changed files with 30 additions and 11 deletions
|
@ -5,6 +5,7 @@ import (
|
|||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"github.com/anyproto/any-sync/app/logger"
|
||||
|
@ -83,11 +84,23 @@ func (s *lumberjackSink) Sync() error {
|
|||
}
|
||||
|
||||
func newLumberjackSink(u *url.URL) (zap.Sink, error) {
|
||||
// if android, limit the log file size to 10MB
|
||||
if runtime.GOOS == "android" || runtime.GOARCH == "ios" {
|
||||
return &lumberjackSink{
|
||||
Logger: &lumberjack.Logger{
|
||||
Filename: u.Path,
|
||||
MaxSize: 10,
|
||||
MaxBackups: 2,
|
||||
Compress: false,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
return &lumberjackSink{
|
||||
Logger: &lumberjack.Logger{
|
||||
Filename: u.Path,
|
||||
MaxSize: 1,
|
||||
MaxAge: 28,
|
||||
Filename: u.Path,
|
||||
MaxSize: 100,
|
||||
MaxBackups: 10,
|
||||
Compress: true,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
@ -95,9 +108,11 @@ func newLumberjackSink(u *url.URL) (zap.Sink, error) {
|
|||
func Init(root string, logLevels string, sendLogs bool, saveLogs bool) {
|
||||
if root != "" {
|
||||
environment.ROOT_PATH = filepath.Join(root, "common")
|
||||
err := os.Mkdir(environment.ROOT_PATH, 0755)
|
||||
err := os.MkdirAll(environment.ROOT_PATH, 0755)
|
||||
if err != nil {
|
||||
fmt.Println("failed to create global dir", err)
|
||||
if !os.IsExist(err) {
|
||||
fmt.Println("failed to create global dir", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -129,15 +144,19 @@ func registerLumberjackSink(globalRoot string, config *logger.Config) {
|
|||
fmt.Println("globalRoot dir is not set")
|
||||
return
|
||||
}
|
||||
err := zap.RegisterSink(lumberjackScheme, newLumberjackSink)
|
||||
logsDir := filepath.Join(globalRoot, "logs")
|
||||
err := os.Mkdir(logsDir, 0755)
|
||||
if err != nil && !os.IsExist(err) {
|
||||
fmt.Println("failed to create logs dir", err)
|
||||
// do not continue if logs dir can't be created
|
||||
return
|
||||
}
|
||||
|
||||
err = zap.RegisterSink(lumberjackScheme, newLumberjackSink)
|
||||
if err != nil {
|
||||
fmt.Println("failed to register lumberjack sink", err)
|
||||
}
|
||||
logsDir := filepath.Join(globalRoot, "logs")
|
||||
err = os.Mkdir(logsDir, 0755)
|
||||
if err != nil {
|
||||
fmt.Println("failed to create globalRoot dir", err)
|
||||
}
|
||||
|
||||
environment.LOG_PATH = filepath.Join(logsDir, "anytype.log")
|
||||
config.AddOutputPaths = append(config.AddOutputPaths, lumberjackScheme+":"+environment.LOG_PATH)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue