Skip to content

Commit

Permalink
Update logging to exp/slog
Browse files Browse the repository at this point in the history
  • Loading branch information
janos committed Dec 11, 2022
1 parent 94401d4 commit 10f3656
Show file tree
Hide file tree
Showing 15 changed files with 153 additions and 595 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.18
go-version: 1.19

- name: Checkout
uses: actions/checkout@v1
Expand All @@ -34,7 +34,7 @@ jobs:
- name: Lint
uses: golangci/golangci-lint-action@v2
with:
version: v1.45.2
version: v1.50.1
args: --timeout 10m

- name: Vet
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.18
go-version: 1.19
-
name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2
Expand Down
31 changes: 7 additions & 24 deletions cmd/compromised/config/compromised.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"path/filepath"

"resenje.org/compromised"
"resenje.org/logging"
"resenje.org/marshal"
)

Expand All @@ -25,15 +24,7 @@ type CompromisedOptions struct {
// Passwords
PasswordsDB string `json:"passwords-db" yaml:"passwords-db" envconfig:"PASSWORDS_DB"`
// Logging
LogDir string `json:"log-dir" yaml:"log-dir" envconfig:"LOG_DIR"`
LogLevel logging.Level `json:"log-level" yaml:"log-level" envconfig:"LOG_LEVEL"`
SyslogFacility logging.SyslogFacility `json:"syslog-facility" yaml:"syslog-facility" envconfig:"SYSLOG_FACILITY"`
SyslogTag string `json:"syslog-tag" yaml:"syslog-tag" envconfig:"SYSLOG_TAG"`
SyslogNetwork string `json:"syslog-network" yaml:"syslog-network" envconfig:"SYSLOG_NETWORK"`
SyslogAddress string `json:"syslog-address" yaml:"syslog-address" envconfig:"SYSLOG_ADDRESS"`
AccessLogLevel logging.Level `json:"access-log-level" yaml:"access-log-level" envconfig:"ACCESS_LOG_LEVEL"`
AccessSyslogFacility logging.SyslogFacility `json:"access-syslog-facility" yaml:"access-syslog-facility" envconfig:"ACCESS_SYSLOG_FACILITY"`
AccessSyslogTag string `json:"access-syslog-tag" yaml:"access-syslog-tag" envconfig:"ACCESS_SYSLOG_TAG"`
LogDir string `json:"log-dir" yaml:"log-dir" envconfig:"LOG_DIR"`
// Daemon
DaemonLogFileName string `json:"daemon-log-file" yaml:"daemon-log-file" envconfig:"DAEMON_LOG_FILE"`
DaemonLogFileMode marshal.Mode `json:"daemon-log-file-mode" yaml:"daemon-log-file-mode" envconfig:"DAEMON_LOG_FILE_MODE"`
Expand All @@ -49,20 +40,12 @@ func NewCompromisedOptions() *CompromisedOptions {
"Server": Name + "/" + compromised.Version(),
"X-Frame-Options": "SAMEORIGIN",
},
RealIPHeaderName: "X-Real-IP",
PasswordsDB: "",
LogDir: "",
LogLevel: logging.DEBUG,
SyslogFacility: "",
SyslogTag: Name,
SyslogNetwork: "",
SyslogAddress: "",
AccessLogLevel: logging.DEBUG,
AccessSyslogFacility: "",
AccessSyslogTag: Name + "-access",
DaemonLogFileName: "daemon.log",
DaemonLogFileMode: 0644,
PidFileName: filepath.Join(os.TempDir(), Name+".pid"),
RealIPHeaderName: "X-Real-IP",
PasswordsDB: "",
LogDir: "",
DaemonLogFileName: "daemon.log",
DaemonLogFileMode: 0644,
PidFileName: filepath.Join(os.TempDir(), Name+".pid"),
}
}

Expand Down
89 changes: 55 additions & 34 deletions cmd/compromised/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import (
"context"
"errors"
"fmt"
"io"
"os"
"sync"
"syscall"
"time"

loggingpromethues "resenje.org/logging/prometheus"
"golang.org/x/exp/slog"
"resenje.org/recovery"
"resenje.org/web/logging"
"resenje.org/web/server"
"resenje.org/x/application"

Expand Down Expand Up @@ -54,41 +54,62 @@ Refer to https://resenje.org/compromised documentation.`)
var shutdownFuncs []func() error

// Setup logging.
loggers := application.NewLoggers(
application.WithForcedWriter(func() io.Writer {
if options.LogDir == "" {
return os.Stderr
}
return nil
}()),
)
loggingCounter := loggingpromethues.NewCounter(&loggingpromethues.CounterOptions{
// loggers := application.NewLoggers(
// application.WithForcedWriter(func() io.Writer {
// if options.LogDir == "" {
// return os.Stderr
// }
// return nil
// }()),
// )
// loggingCounter := loggingpromethues.NewCounter(&loggingpromethues.CounterOptions{
// Namespace: metrics.Namespace,
// })
// logger := loggers.NewLogger("default", options.LogLevel,
// application.NewTimedFileHandler(options.LogDir, config.Name),
// application.NewSyslogHandler(
// options.SyslogFacility,
// options.SyslogTag,
// options.SyslogNetwork,
// options.SyslogAddress,
// ),
// loggingpromethues.NewHandler(loggingCounter, options.LogLevel),
// )
// application.SetStdLogger()
// accessLogger := loggers.NewLogger("access", options.AccessLogLevel,
// application.NewTimedFileHandler(options.LogDir, "access"),
// application.NewSyslogHandler(
// options.AccessSyslogFacility,
// options.AccessSyslogTag,
// options.SyslogNetwork,
// options.SyslogAddress,
// ),
// )

loggingMetrics := logging.NewMetrics(&logging.MetricsOptions{
Namespace: metrics.Namespace,
})
logger := loggers.NewLogger("default", options.LogLevel,
application.NewTimedFileHandler(options.LogDir, config.Name),
application.NewSyslogHandler(
options.SyslogFacility,
options.SyslogTag,
options.SyslogNetwork,
options.SyslogAddress,
),
loggingpromethues.NewHandler(loggingCounter, options.LogLevel),
)
application.SetStdLogger()
accessLogger := loggers.NewLogger("access", options.AccessLogLevel,
application.NewTimedFileHandler(options.LogDir, "access"),
application.NewSyslogHandler(
options.AccessSyslogFacility,
options.AccessSyslogTag,
options.SyslogNetwork,
options.SyslogAddress,
),
)

loggerWriter := logging.ApplicationLogWriteCloser(options.LogDir, config.Name, os.Stderr)
defer loggerWriter.Close()
logger := slog.New(slog.HandlerOptions{
ReplaceAttr: func(a slog.Attr) slog.Attr {
if a.Key == slog.LevelKey {
loggingMetrics.Inc(a.Value.Any().(slog.Level))
}
return a
},
}.NewTextHandler(loggerWriter))

slog.SetDefault(logger)

accessLoggerWriter := logging.ApplicationLogWriteCloser(options.LogDir, "access", os.Stderr)
defer accessLoggerWriter.Close()
accessLogger := slog.New(slog.NewTextHandler(accessLoggerWriter))

// Log application version on start
app.Functions = append(app.Functions, func() (err error) {
logger.Infof("version: %v", compromised.Version())
logger.Info("start", "version", compromised.Version())
return nil
})

Expand All @@ -109,7 +130,7 @@ Refer to https://resenje.org/compromised documentation.`)
if err != nil {
return fmt.Errorf("server: %w", err)
}
srv.WithMetrics(loggingCounter.Metrics()...)
srv.WithMetrics(loggingMetrics.Metrics()...)

passwordsService, err := filepasswords.New(options.PasswordsDB)
if err != nil {
Expand Down Expand Up @@ -160,7 +181,7 @@ Refer to https://resenje.org/compromised documentation.`)
go func(shutdown func() error) {
defer wg.Done()
if err := shutdown(); err != nil {
logger.Errorf("shutting down: %v", err)
logger.Error("shutdown", err)
}
}(shutdown)
}
Expand Down
34 changes: 18 additions & 16 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,37 +1,39 @@
module resenje.org/compromised

go 1.18
go 1.19

require (
github.com/felixge/httpsnoop v1.0.1
github.com/gorilla/handlers v1.5.1
github.com/gorilla/mux v1.8.0
github.com/prometheus/client_golang v1.12.1
github.com/prometheus/client_golang v1.14.0
golang.org/x/exp v0.0.0-20221208152030-732eee02a75a
resenje.org/daemon v0.1.2
resenje.org/jsonhttp v0.2.0
resenje.org/logging v0.1.7
resenje.org/marshal v0.1.1
resenje.org/recovery v0.1.1
resenje.org/web v0.7.0
resenje.org/x v0.3.5
resenje.org/web v0.8.0
resenje.org/x v0.5.1
)

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/felixge/httpsnoop v1.0.3 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/kelseyhightower/envconfig v1.4.0 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.32.1 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
golang.org/x/crypto v0.0.0-20220427172511-eb4f295cb31f // indirect
golang.org/x/net v0.0.0-20220425223048-2871e0cb64e4 // indirect
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9 // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/protobuf v1.28.0 // indirect
github.com/kr/text v0.1.0 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/common v0.38.0 // indirect
github.com/prometheus/procfs v0.8.0 // indirect
golang.org/x/crypto v0.4.0 // indirect
golang.org/x/net v0.4.0 // indirect
golang.org/x/sys v0.3.0 // indirect
golang.org/x/text v0.5.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
gopkg.in/mail.v2 v2.3.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
resenje.org/email v0.1.3 // indirect
resenje.org/iostuff v0.1.3 // indirect
)
Loading

0 comments on commit 10f3656

Please sign in to comment.