Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
20 changes: 20 additions & 0 deletions cli/deployment/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,26 @@ func newConfig() *codersdk.DeploymentConfig {
Default: false,
},
},
Logging: &codersdk.LoggingConfig{
Human: &codersdk.DeploymentConfigField[string]{
Name: "Human Log Location",
Usage: "Output human-readable logs to a given file.",
Flag: "log-human",
Default: "/dev/stderr",
},
JSON: &codersdk.DeploymentConfigField[string]{
Name: "JSON Log Location",
Usage: "Output JSON logs to a given file.",
Flag: "log-json",
Default: "",
},
Stackdriver: &codersdk.DeploymentConfigField[string]{
Name: "Stackdriver Log Location",
Usage: "Output Stackdriver compatible logs to a given file.",
Flag: "log-stackdriver",
Default: "",
},
},
}
}

Expand Down
82 changes: 76 additions & 6 deletions cli/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ import (

"cdr.dev/slog"
"cdr.dev/slog/sloggers/sloghuman"
"cdr.dev/slog/sloggers/slogjson"
"cdr.dev/slog/sloggers/slogstackdriver"
"github.com/coder/coder/buildinfo"
"github.com/coder/coder/cli/cliui"
"github.com/coder/coder/cli/config"
Expand Down Expand Up @@ -122,13 +124,11 @@ func Server(vip *viper.Viper, newAPI func(context.Context, *coderd.Options) (*co
}

printLogo(cmd)
logger := slog.Make(sloghuman.Sink(cmd.ErrOrStderr()))
if ok, _ := cmd.Flags().GetBool(varVerbose); ok {
logger = logger.Leveled(slog.LevelDebug)
}
if cfg.Trace.CaptureLogs.Value {
logger = logger.AppendSinks(tracing.SlogSink{})
logger, logCloser, err := makeLogger(cmd, cfg)
if err != nil {
return xerrors.Errorf("make logger: %w", err)
}
defer logCloser()

// Register signals early on so that graceful shutdown can't
// be interrupted by additional signals. Note that we avoid
Expand Down Expand Up @@ -1145,6 +1145,11 @@ func newProvisionerDaemon(

// nolint: revive
func printLogo(cmd *cobra.Command) {
// Only print the logo in TTYs.
if !isTTYOut(cmd) {
return
}

_, _ = fmt.Fprintf(cmd.OutOrStdout(), "%s - Software development on your infrastucture\n", cliui.Styles.Bold.Render("Coder "+buildinfo.Version()))
}

Expand Down Expand Up @@ -1512,3 +1517,68 @@ func redirectHTTPToAccessURL(handler http.Handler, accessURL *url.URL) http.Hand
func isLocalhost(host string) bool {
return host == "localhost" || host == "127.0.0.1" || host == "::1"
}

func makeLogger(cmd *cobra.Command, cfg *codersdk.DeploymentConfig) (slog.Logger, func(), error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add testing around this. If logging breaks in any way, we really hurt our customers.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@coadler since you can't read sinks from an instantiated logger, you could make this return ([]slog.Sink, slog.Level, func() error) instead and instantiate the logger from the caller.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like that!

var (
sinks = []slog.Sink{}
closers = []func() error{}
)

if cfg.Logging.Human.Value != "" {
if cfg.Logging.Human.Value == "/dev/stderr" {
sinks = append(sinks, sloghuman.Sink(cmd.ErrOrStderr()))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice touch 👍

} else {
fi, err := os.OpenFile(cfg.Logging.Human.Value, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
return slog.Logger{}, nil, xerrors.Errorf("open human log %q: %w", cfg.Logging.Human.Value, err)
}
closers = append(closers, fi.Close)
sinks = append(sinks, sloghuman.Sink(fi))
}
}

if cfg.Logging.JSON.Value != "" {
if cfg.Logging.JSON.Value == "/dev/stderr" {
sinks = append(sinks, slogjson.Sink(cmd.ErrOrStderr()))
} else {
fi, err := os.OpenFile(cfg.Logging.JSON.Value, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
return slog.Logger{}, nil, xerrors.Errorf("open json log %q: %w", cfg.Logging.JSON.Value, err)
}
closers = append(closers, fi.Close)
sinks = append(sinks, slogjson.Sink(fi))
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This whole routine could be made into a little helper function (even just a function stored in a variable at the top of this function would be good) that returns an io.Writer

}

if cfg.Logging.Stackdriver.Value != "" {
if cfg.Logging.JSON.Value == "/dev/stderr" {
sinks = append(sinks, slogstackdriver.Sink(cmd.ErrOrStderr()))
} else {
fi, err := os.OpenFile(cfg.Logging.Stackdriver.Value, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
return slog.Logger{}, nil, xerrors.Errorf("open stackdriver log %q: %w", cfg.Logging.Stackdriver.Value, err)
}
closers = append(closers, fi.Close)
sinks = append(sinks, slogstackdriver.Sink(fi))
}
}

if cfg.Trace.CaptureLogs.Value {
sinks = append(sinks, tracing.SlogSink{})
}

level := slog.LevelInfo
if ok, _ := cmd.Flags().GetBool(varVerbose); ok {
level = slog.LevelDebug
}

if len(sinks) == 0 {
return slog.Logger{}, nil, xerrors.New("no loggers provided")
}

return slog.Make(sinks...).Leveled(level), func() {
for _, closer := range closers {
_ = closer()
}
}, nil
}
9 changes: 9 additions & 0 deletions cli/testdata/coder_server_--help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ Flags:
disable the HTTP endpoint.
Consumes $CODER_HTTP_ADDRESS (default
"127.0.0.1:3000")
--log-human string Output human-readable logs to a given
file.
Consumes $CODER_LOGGING_HUMAN (default
"/dev/stderr")
--log-json string Output JSON logs to a given file.
Consumes $CODER_LOGGING_JSON
--log-stackdriver string Output Stackdriver compatible logs to a
given file.
Consumes $CODER_LOGGING_STACKDRIVER
--max-token-lifetime duration The maximum lifetime duration for any
user creating a token.
Consumes $CODER_MAX_TOKEN_LIFETIME
Expand Down
17 changes: 17 additions & 0 deletions coderd/apidoc/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5362,6 +5362,9 @@ const docTemplate = `{
"in_memory_database": {
"$ref": "#/definitions/codersdk.DeploymentConfigField-bool"
},
"logging": {
"$ref": "#/definitions/codersdk.LoggingConfig"
},
"max_token_lifetime": {
"$ref": "#/definitions/codersdk.DeploymentConfigField-time_Duration"
},
Expand Down Expand Up @@ -5881,6 +5884,20 @@ const docTemplate = `{
"LogSourceProvisioner"
]
},
"codersdk.LoggingConfig": {
"type": "object",
"properties": {
"human": {
"$ref": "#/definitions/codersdk.DeploymentConfigField-string"
},
"json": {
"$ref": "#/definitions/codersdk.DeploymentConfigField-string"
},
"stackdriver": {
"$ref": "#/definitions/codersdk.DeploymentConfigField-string"
}
}
},
"codersdk.LoginType": {
"type": "string",
"enum": [
Expand Down
17 changes: 17 additions & 0 deletions coderd/apidoc/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -4757,6 +4757,9 @@
"in_memory_database": {
"$ref": "#/definitions/codersdk.DeploymentConfigField-bool"
},
"logging": {
"$ref": "#/definitions/codersdk.LoggingConfig"
},
"max_token_lifetime": {
"$ref": "#/definitions/codersdk.DeploymentConfigField-time_Duration"
},
Expand Down Expand Up @@ -5258,6 +5261,20 @@
"enum": ["provisioner_daemon", "provisioner"],
"x-enum-varnames": ["LogSourceProvisionerDaemon", "LogSourceProvisioner"]
},
"codersdk.LoggingConfig": {
"type": "object",
"properties": {
"human": {
"$ref": "#/definitions/codersdk.DeploymentConfigField-string"
},
"json": {
"$ref": "#/definitions/codersdk.DeploymentConfigField-string"
},
"stackdriver": {
"$ref": "#/definitions/codersdk.DeploymentConfigField-string"
}
}
},
"codersdk.LoginType": {
"type": "string",
"enum": ["password", "github", "oidc", "token"],
Expand Down
7 changes: 7 additions & 0 deletions codersdk/deploymentconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type DeploymentConfig struct {
UpdateCheck *DeploymentConfigField[bool] `json:"update_check" typescript:",notnull"`
MaxTokenLifetime *DeploymentConfigField[time.Duration] `json:"max_token_lifetime" typescript:",notnull"`
Swagger *SwaggerConfig `json:"swagger" typescript:",notnull"`
Logging *LoggingConfig `json:"logging" typescript:",notnull"`
}

type DERP struct {
Expand Down Expand Up @@ -155,6 +156,12 @@ type SwaggerConfig struct {
Enable *DeploymentConfigField[bool] `json:"enable" typescript:",notnull"`
}

type LoggingConfig struct {
Human *DeploymentConfigField[string] `json:"human" typescript:",notnull"`
JSON *DeploymentConfigField[string] `json:"json" typescript:",notnull"`
Stackdriver *DeploymentConfigField[string] `json:"stackdriver" typescript:",notnull"`
}

type Flaggable interface {
string | time.Duration | bool | int | []string | []GitAuthConfig
}
Expand Down
35 changes: 35 additions & 0 deletions docs/api/general.md
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,41 @@ curl -X GET http://coder-server:8080/api/v2/config/deployment \
"usage": "string",
"value": true
},
"logging": {
"human": {
"default": "string",
"enterprise": true,
"flag": "string",
"hidden": true,
"name": "string",
"secret": true,
"shorthand": "string",
"usage": "string",
"value": "string"
},
"json": {
"default": "string",
"enterprise": true,
"flag": "string",
"hidden": true,
"name": "string",
"secret": true,
"shorthand": "string",
"usage": "string",
"value": "string"
},
"stackdriver": {
"default": "string",
"enterprise": true,
"flag": "string",
"hidden": true,
"name": "string",
"secret": true,
"shorthand": "string",
"usage": "string",
"value": "string"
}
},
"max_token_lifetime": {
"default": 0,
"enterprise": true,
Expand Down
84 changes: 84 additions & 0 deletions docs/api/schemas.md
Original file line number Diff line number Diff line change
Expand Up @@ -1408,6 +1408,41 @@ CreateParameterRequest is a structure used to create a new parameter value for a
"usage": "string",
"value": true
},
"logging": {
"human": {
"default": "string",
"enterprise": true,
"flag": "string",
"hidden": true,
"name": "string",
"secret": true,
"shorthand": "string",
"usage": "string",
"value": "string"
},
"json": {
"default": "string",
"enterprise": true,
"flag": "string",
"hidden": true,
"name": "string",
"secret": true,
"shorthand": "string",
"usage": "string",
"value": "string"
},
"stackdriver": {
"default": "string",
"enterprise": true,
"flag": "string",
"hidden": true,
"name": "string",
"secret": true,
"shorthand": "string",
"usage": "string",
"value": "string"
}
},
"max_token_lifetime": {
"default": 0,
"enterprise": true,
Expand Down Expand Up @@ -2022,6 +2057,7 @@ CreateParameterRequest is a structure used to create a new parameter value for a
| `gitauth` | [codersdk.DeploymentConfigField-array_codersdk_GitAuthConfig](#codersdkdeploymentconfigfield-array_codersdk_gitauthconfig) | false | | |
| `http_address` | [codersdk.DeploymentConfigField-string](#codersdkdeploymentconfigfield-string) | false | | |
| `in_memory_database` | [codersdk.DeploymentConfigField-bool](#codersdkdeploymentconfigfield-bool) | false | | |
| `logging` | [codersdk.LoggingConfig](#codersdkloggingconfig) | false | | |
| `max_token_lifetime` | [codersdk.DeploymentConfigField-time_Duration](#codersdkdeploymentconfigfield-time_duration) | false | | |
| `metrics_cache_refresh_interval` | [codersdk.DeploymentConfigField-time_Duration](#codersdkdeploymentconfigfield-time_duration) | false | | |
| `oauth2` | [codersdk.OAuth2Config](#codersdkoauth2config) | false | | |
Expand Down Expand Up @@ -2558,6 +2594,54 @@ CreateParameterRequest is a structure used to create a new parameter value for a
| `provisioner_daemon` |
| `provisioner` |

## codersdk.LoggingConfig

```json
{
"human": {
"default": "string",
"enterprise": true,
"flag": "string",
"hidden": true,
"name": "string",
"secret": true,
"shorthand": "string",
"usage": "string",
"value": "string"
},
"json": {
"default": "string",
"enterprise": true,
"flag": "string",
"hidden": true,
"name": "string",
"secret": true,
"shorthand": "string",
"usage": "string",
"value": "string"
},
"stackdriver": {
"default": "string",
"enterprise": true,
"flag": "string",
"hidden": true,
"name": "string",
"secret": true,
"shorthand": "string",
"usage": "string",
"value": "string"
}
}
```

### Properties

| Name | Type | Required | Restrictions | Description |
| ------------- | ------------------------------------------------------------------------------ | -------- | ------------ | ----------- |
| `human` | [codersdk.DeploymentConfigField-string](#codersdkdeploymentconfigfield-string) | false | | |
| `json` | [codersdk.DeploymentConfigField-string](#codersdkdeploymentconfigfield-string) | false | | |
| `stackdriver` | [codersdk.DeploymentConfigField-string](#codersdkdeploymentconfigfield-string) | false | | |

## codersdk.LoginType

```json
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ require (
tailscale.com v1.32.2
)

require cloud.google.com/go/longrunning v0.1.1 // indirect

require (
cloud.google.com/go/compute v1.12.1 // indirect
filippo.io/edwards25519 v1.0.0-rc.1 // indirect
Expand Down
Loading