-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add auto gomemlimit and switch to zap logging
Signed-off-by: Markus Blaschke <[email protected]>
- Loading branch information
Showing
8 changed files
with
176 additions
and
94 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package main | ||
|
||
import ( | ||
"log/slog" | ||
|
||
"go.uber.org/zap" | ||
"go.uber.org/zap/exp/zapslog" | ||
"go.uber.org/zap/zapcore" | ||
) | ||
|
||
var ( | ||
logger *zap.SugaredLogger | ||
slogger *slog.Logger | ||
) | ||
|
||
func initLogger() *zap.SugaredLogger { | ||
var config zap.Config | ||
if Opts.Logger.Development { | ||
config = zap.NewDevelopmentConfig() | ||
config.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder | ||
} else { | ||
config = zap.NewProductionConfig() | ||
} | ||
|
||
config.Encoding = "console" | ||
config.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder | ||
|
||
// debug level | ||
if Opts.Logger.Debug { | ||
config.Level = zap.NewAtomicLevelAt(zapcore.DebugLevel) | ||
} | ||
|
||
// json log format | ||
if Opts.Logger.Json { | ||
config.Encoding = "json" | ||
|
||
// if running in containers, logs already enriched with timestamp by the container runtime | ||
config.EncoderConfig.TimeKey = "" | ||
} | ||
|
||
// build logger | ||
log, err := config.Build() | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
logger = log.Sugar() | ||
slogger = slog.New(zapslog.NewHandler(log.Core(), nil)) | ||
|
||
return logger | ||
} |
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/KimMachineGun/automemlimit/memlimit" | ||
humanize "github.com/dustin/go-humanize" | ||
) | ||
|
||
func initSystem() { | ||
// set memory limit | ||
goMemLimit, err := memlimit.SetGoMemLimitWithOpts( | ||
memlimit.WithProvider( | ||
memlimit.ApplyFallback( | ||
memlimit.FromCgroup, | ||
memlimit.FromSystem, | ||
), | ||
), | ||
memlimit.WithLogger(slogger), | ||
) | ||
|
||
if goMemLimit > 0 { | ||
logger.Infof(`GOMEMLIMIT updated to %v`, humanize.Bytes(uint64(goMemLimit))) | ||
} | ||
|
||
if err != nil { | ||
logger.Fatal(err) | ||
} | ||
} |
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
Oops, something went wrong.