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

Add cli flag to enable structured logging #1598

Merged
merged 5 commits into from
Jan 17, 2023
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
2 changes: 1 addition & 1 deletion cli/cmd/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func BuildCmd(ver version.Version) *cobra.Command {
Use: "build",
Short: "Build project without starting web app",
RunE: func(cmd *cobra.Command, args []string) error {
app, err := local.NewApp(cmd.Context(), ver, verbose, olapDriver, olapDSN, projectPath)
app, err := local.NewApp(cmd.Context(), ver, verbose, olapDriver, olapDSN, projectPath, local.LogFormatColor)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/initialize/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func InitCmd(ver version.Version) *cobra.Command {
fmt.Println("You can reach us in our Rill Discord server at https://bit.ly/3NSMKdT.")
fmt.Println("")

app, err := local.NewApp(cmd.Context(), ver, verbose, olapDriver, olapDSN, projectPath)
app, err := local.NewApp(cmd.Context(), ver, verbose, olapDriver, olapDSN, projectPath, local.LogFormatColor)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/source/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func AddCmd(ver version.Version) *cobra.Command {
dataPath = relPath
}

app, err := local.NewApp(cmd.Context(), ver, verbose, olapDriver, olapDSN, projectPath)
app, err := local.NewApp(cmd.Context(), ver, verbose, olapDriver, olapDSN, projectPath, local.LogFormatColor)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/source/drop.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func DropCmd(ver version.Version) *cobra.Command {
return fmt.Errorf("not a valid source name: %s", sourceName)
}

app, err := local.NewApp(cmd.Context(), ver, verbose, olapDriver, olapDSN, projectPath)
app, err := local.NewApp(cmd.Context(), ver, verbose, olapDriver, olapDSN, projectPath, local.LogFormatColor)
if err != nil {
return err
}
Expand Down
9 changes: 8 additions & 1 deletion cli/cmd/start/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,18 @@ func StartCmd(ver version.Version) *cobra.Command {
var verbose bool
var noUI bool
var noOpen bool
var format string

startCmd := &cobra.Command{
Use: "start",
Short: "Build project and start web app",
RunE: func(cmd *cobra.Command, args []string) error {
app, err := local.NewApp(cmd.Context(), ver, verbose, olapDriver, olapDSN, projectPath)
logFormat, ok := local.ParseLogFormat(format)
if !ok {
logFormat = local.LogFormatColor
}

app, err := local.NewApp(cmd.Context(), ver, verbose, olapDriver, olapDSN, projectPath, logFormat)
if err != nil {
return err
}
Expand Down Expand Up @@ -60,6 +66,7 @@ func StartCmd(ver version.Version) *cobra.Command {
startCmd.Flags().IntVar(&grpcPort, "port-grpc", 9010, "Port for gRPC")
startCmd.Flags().BoolVar(&noUI, "no-ui", false, "Serve only the backend")
startCmd.Flags().BoolVar(&verbose, "verbose", false, "Sets the log level to debug")
startCmd.Flags().StringVar(&format, "log-format", "color", "Sets the log format to color/json")

return startCmd
}
34 changes: 31 additions & 3 deletions cli/pkg/local/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ import (
_ "github.com/rilldata/rill/runtime/drivers/sqlite"
)

type LogFormat string

// Default log formats for logger
const (
LogFormatColor = "color"
LogFormatJSON = "json"
)

// Default instance config on local.
const (
DefaultInstanceID = "default"
Expand All @@ -57,12 +65,21 @@ type App struct {
ProjectPath string
}

func NewApp(ctx context.Context, ver version.Version, verbose bool, olapDriver, olapDSN, projectPath string) (*App, error) {
// Setup a friendly-looking colored logger
func NewApp(ctx context.Context, ver version.Version, verbose bool, olapDriver, olapDSN, projectPath string, logFormat LogFormat) (*App, error) {
// Setup a friendly-looking colored/json logger
var encoder zapcore.Encoder
conf := zap.NewDevelopmentEncoderConfig()
conf.EncodeLevel = zapcore.CapitalColorLevelEncoder

switch logFormat {
case LogFormatJSON:
encoder = zapcore.NewJSONEncoder(conf)
case LogFormatColor:
encoder = zapcore.NewConsoleEncoder(conf)
}

logger := zap.New(zapcore.NewCore(
zapcore.NewConsoleEncoder(conf),
encoder,
zapcore.AddSync(colorable.NewColorableStdout()),
zapcore.DebugLevel,
))
Expand Down Expand Up @@ -440,3 +457,14 @@ func cors(h http.Handler) http.Handler {
h.ServeHTTP(w, r)
})
}

func ParseLogFormat(format string) (LogFormat, bool) {
switch format {
case "json":
return LogFormatJSON, true
case "color":
return LogFormatColor, true
default:
return "", false
}
}