Skip to content

[!] implement enhanced logging #146

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

Merged
merged 4 commits into from
Mar 17, 2023
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 .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"--dbname=pgwatch3",
"--datastore=postgres",
"--pg-metric-store-conn-str=postgresql://pgwatch3:pgwatch3@localhost:5432/pgwatch3_metrics",
"--verbose=info"
"--log-level=debug"
]
}
]
Expand Down
4 changes: 2 additions & 2 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"options": {
"cwd": "${workspaceFolder}/src/"
},
"command": "gotest -failfast -timeout=300s -parallel=1 ..${pathSeparator}${relativeFileDirname}${pathSeparator}... -coverprofile='coverage.out' && go tool cover -func='coverage.out'",
"command": "go test -failfast -timeout=300s -parallel=1 ..${pathSeparator}${relativeFileDirname}${pathSeparator}... -coverprofile='coverage.out' -json | tparse -all",
"problemMatcher": [
"$go"
],
Expand All @@ -26,7 +26,7 @@
{
"label": "Coverage Report",
"type": "shell",
"command": "cd src && go tool cover -html='coverage.out'",
"command": "cd src && go tool cover -func='coverage.out' && go tool cover -html='coverage.out'",
"problemMatcher": [
"$go"
],
Expand Down
2 changes: 1 addition & 1 deletion src/.gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Binaries for programs and plugins
pg_timetable
pgwatch3
*.exe
*.exe~
*.dll
Expand Down
20 changes: 19 additions & 1 deletion src/config/cmdparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,23 @@ type MetricOpts struct {
JSONStorageFile string `long:"json-storage-file" mapstructure:"json-storage-file" description:"Path to file where metrics will be stored when --datastore=json, one metric set per line" env:"PW3_JSON_STORAGE_FILE"`
}

// LoggingOpts specifies the logging configuration
type LoggingOpts struct {
// LogDBLevel string `long:"log-database-level" mapstructure:"log-database-level" description:"Verbosity level for database storing" choice:"debug" choice:"info" choice:"error" choice:"none" default:"info"`
LogLevel string `short:"v" long:"log-level" mapstructure:"log-level" description:"Verbosity level for stdout and log file" choice:"debug" choice:"info" choice:"error" default:"info"`
LogFile string `long:"log-file" mapstructure:"log-file" description:"File name to store logs"`
LogFileFormat string `long:"log-file-format" mapstructure:"log-file-format" description:"Format of file logs" choice:"json" choice:"text" default:"json"`
LogFileRotate bool `long:"log-file-rotate" mapstructure:"log-file-rotate" description:"Rotate log files"`
LogFileSize int `long:"log-file-size" mapstructure:"log-file-size" description:"Maximum size in MB of the log file before it gets rotated" default:"100"`
LogFileAge int `long:"log-file-age" mapstructure:"log-file-age" description:"Number of days to retain old log files, 0 means forever" default:"0"`
LogFileNumber int `long:"log-file-number" mapstructure:"log-file-number" description:"Maximum number of old log files to retain, 0 to retain all" default:"0"`
}

type CmdOptions struct {
Connection ConnectionOpts `group:"Connection" mapstructure:"Connection"`
Metric MetricOpts `group:"Metric" mapstructure:"Metric"`
Verbose string `short:"v" long:"verbose" mapstructure:"verbose" description:"Chat level [DEBUG|INFO|WARN]. Default: WARN" env:"PW3_VERBOSE"`
Logging LoggingOpts `group:"Logging" mapstructure:"Logging"`
// Verbose string `short:"v" long:"verbose" mapstructure:"verbose" description:"Chat level [DEBUG|INFO|WARN]. Default: WARN" env:"PW3_VERBOSE"`
// Params for running based on local config files, enabled distributed "push model" based metrics gathering. Metrics are sent directly to Influx/Graphite.
Config string `short:"c" long:"config" mapstructure:"config" description:"File or folder of YAML files containing info on which DBs to monitor and where to store metrics" env:"PW3_CONFIG"`
BatchingDelayMs int64 `long:"batching-delay-ms" mapstructure:"batching-delay-ms" description:"Max milliseconds to wait for a batched metrics flush. [Default: 250]" default:"250" env:"PW3_BATCHING_MAX_DELAY_MS"`
Expand Down Expand Up @@ -69,6 +82,11 @@ type CmdOptions struct {
TryCreateListedExtsIfMissing string `long:"try-create-listed-exts-if-missing" mapstructure:"try-create-listed-exts-if-missing" description:"Try creating the listed extensions (comma sep.) on first connect for all monitored DBs when missing. Main usage - pg_stat_statements" env:"PW3_TRY_CREATE_LISTED_EXTS_IF_MISSING" default:""`
}

// Verbose returns true if the debug log is enabled
func (c CmdOptions) Verbose() bool {
return c.Logging.LogLevel == "debug"
}

func (c CmdOptions) IsAdHocMode() bool {
return len(c.AdHocConnString)+len(c.AdHocConfig) > 0
}
Expand Down
Loading