Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make log rotator params configurable. #249

Merged
merged 1 commit into from
May 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
ISC License

Copyright (c) 2020 The Decred developers
Copyright (c) 2020-2021 The Decred developers

Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
Expand Down
8 changes: 7 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import (
var (
defaultListen = ":8800"
defaultLogLevel = "debug"
defaultMaxLogSize = int64(10)
defaultLogsToKeep = 20
defaultVSPFee = 3.0
defaultNetwork = "testnet"
defaultHomeDir = dcrutil.AppDataDir("vspd", false)
Expand All @@ -44,6 +46,8 @@ var (
type config struct {
Listen string `long:"listen" ini-name:"listen" description:"The ip:port to listen for API requests."`
LogLevel string `long:"loglevel" ini-name:"loglevel" description:"Logging level." choice:"trace" choice:"debug" choice:"info" choice:"warn" choice:"error" choice:"critical"`
MaxLogSize int64 `long:"maxlogsize" ini-name:"maxlogsize" description:"File size threshold for log file rotation (MB)."`
LogsToKeep int `long:"logstokeep" ini-name:"logstokeep" description:"The number of rotated log files to keep."`
Network string `long:"network" ini-name:"network" description:"Decred network to use." choice:"testnet" choice:"mainnet" choice:"simnet"`
VSPFee float64 `long:"vspfee" ini-name:"vspfee" description:"Fee percentage charged for VSP use. eg. 2.0 (2%), 0.5 (0.5%)."`
DcrdHost string `long:"dcrdhost" ini-name:"dcrdhost" description:"The ip:port to establish a JSON-RPC connection with dcrd. Should be the same host where vspd is running."`
Expand Down Expand Up @@ -164,6 +168,8 @@ func loadConfig() (*config, error) {
cfg := config{
Listen: defaultListen,
LogLevel: defaultLogLevel,
MaxLogSize: defaultMaxLogSize,
LogsToKeep: defaultLogsToKeep,
Network: defaultNetwork,
VSPFee: defaultVSPFee,
HomeDir: defaultHomeDir,
Expand Down Expand Up @@ -392,7 +398,7 @@ func loadConfig() (*config, error) {

// Initialize loggers and log rotation.
logDir := filepath.Join(cfg.HomeDir, "logs", cfg.netParams.Name)
initLogRotator(filepath.Join(logDir, "vspd.log"))
initLogRotator(filepath.Join(logDir, "vspd.log"), cfg.MaxLogSize, cfg.LogsToKeep)
setLogLevels(cfg.LogLevel)

// Set the database path
Expand Down
6 changes: 3 additions & 3 deletions log.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2020 The Decred developers
// Copyright (c) 2020-2021 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

Expand Down Expand Up @@ -72,14 +72,14 @@ var subsystemLoggers = map[string]slog.Logger{
// initLogRotator initializes the logging rotater to write logs to logFile and
// create roll files in the same directory. It must be called before the
// package-global log rotater variables are used.
func initLogRotator(logFile string) {
func initLogRotator(logFile string, maxLogSize int64, logsToKeep int) {
logDir, _ := filepath.Split(logFile)
err := os.MkdirAll(logDir, 0700)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to create log directory: %v\n", err)
os.Exit(1)
}
r, err := rotator.New(logFile, 10*1024, false, 3)
r, err := rotator.New(logFile, maxLogSize*1024, false, logsToKeep)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to create file rotator: %v\n", err)
os.Exit(1)
Expand Down
2 changes: 1 addition & 1 deletion webapi/templates/footer.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

<div class="col-md-4 col-12 footer__credit d-flex justify-content-center align-items-center">
<p class="py-4 m-0">
Decred Developers | 2020
Decred Developers | 2020-2021
<br />
The source code is available on <a href="https://github.com/decred/vspd" target="_blank" rel="noopener noreferrer">GitHub</a>
</p>
Expand Down