Skip to content

Commit

Permalink
Move logdestination flag to sharedRunConfig (#1294)
Browse files Browse the repository at this point in the history
Signed-off-by: Pravin Pushkar <ppushkar@microsoft.com>
  • Loading branch information
pravinpushkar authored May 31, 2023
1 parent af9030a commit c65d816
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 56 deletions.
16 changes: 8 additions & 8 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -579,17 +579,17 @@ func executeRun(runTemplateName, runFilePath string, apps []runfileconfig.App) (
func getAppDaprdWriter(app runfileconfig.App, isAppCommandEmpty bool) io.Writer {
var appDaprdWriter io.Writer
if isAppCommandEmpty {
if app.DaprdLogDestination != runfileconfig.Console {
if app.DaprdLogDestination != standalone.Console {
appDaprdWriter = io.MultiWriter(os.Stdout, app.DaprdLogWriteCloser)
} else {
appDaprdWriter = os.Stdout
}
} else {
if app.AppLogDestination != runfileconfig.Console && app.DaprdLogDestination != runfileconfig.Console {
if app.AppLogDestination != standalone.Console && app.DaprdLogDestination != standalone.Console {
appDaprdWriter = io.MultiWriter(app.AppLogWriteCloser, app.DaprdLogWriteCloser, os.Stdout)
} else if app.AppLogDestination != runfileconfig.Console {
} else if app.AppLogDestination != standalone.Console {
appDaprdWriter = io.MultiWriter(app.AppLogWriteCloser, os.Stdout)
} else if app.DaprdLogDestination != runfileconfig.Console {
} else if app.DaprdLogDestination != standalone.Console {
appDaprdWriter = io.MultiWriter(app.DaprdLogWriteCloser, os.Stdout)
} else {
appDaprdWriter = os.Stdout
Expand All @@ -599,14 +599,14 @@ func getAppDaprdWriter(app runfileconfig.App, isAppCommandEmpty bool) io.Writer
}

// getLogWriter returns the log writer based on the log destination.
func getLogWriter(fileLogWriterCloser io.WriteCloser, logDestination runfileconfig.LogDestType) io.Writer {
func getLogWriter(fileLogWriterCloser io.WriteCloser, logDestination standalone.LogDestType) io.Writer {
var logWriter io.Writer
switch logDestination {
case runfileconfig.Console:
case standalone.Console:
logWriter = os.Stdout
case runfileconfig.File:
case standalone.File:
logWriter = fileLogWriterCloser
case runfileconfig.FileAndConsole:
case standalone.FileAndConsole:
logWriter = io.MultiWriter(os.Stdout, fileLogWriterCloser)
}
return logWriter
Expand Down
64 changes: 43 additions & 21 deletions pkg/standalone/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,15 @@ import (
"github.com/dapr/dapr/pkg/components"
)

type LogDestType string

const (
Console LogDestType = "console"
File LogDestType = "file"
FileAndConsole LogDestType = "fileAndConsole"
DefaultDaprdLogDest = File
DefaultAppLogDest = FileAndConsole

sentryDefaultAddress = "localhost:50001"
defaultStructTagKey = "default"
)
Expand All @@ -52,27 +60,29 @@ type RunConfig struct {

// SharedRunConfig represents the application configuration parameters, which can be shared across many apps.
type SharedRunConfig struct {
ConfigFile string `arg:"config" yaml:"configFilePath"`
AppProtocol string `arg:"app-protocol" yaml:"appProtocol" default:"http"`
APIListenAddresses string `arg:"dapr-listen-addresses" yaml:"apiListenAddresses"`
EnableProfiling bool `arg:"enable-profiling" yaml:"enableProfiling"`
LogLevel string `arg:"log-level" yaml:"logLevel"`
MaxConcurrency int `arg:"app-max-concurrency" yaml:"appMaxConcurrency" default:"-1"`
PlacementHostAddr string `arg:"placement-host-address" yaml:"placementHostAddress"`
ComponentsPath string `arg:"components-path"` // Deprecated in run template file: use ResourcesPaths instead.
ResourcesPath string `yaml:"resourcesPath"` // Deprecated in run template file: use ResourcesPaths instead.
ResourcesPaths []string `arg:"resources-path" yaml:"resourcesPaths"`
AppSSL bool `arg:"app-ssl" yaml:"appSSL"`
MaxRequestBodySize int `arg:"dapr-http-max-request-size" yaml:"daprHTTPMaxRequestSize" default:"-1"`
HTTPReadBufferSize int `arg:"dapr-http-read-buffer-size" yaml:"daprHTTPReadBufferSize" default:"-1"`
EnableAppHealth bool `arg:"enable-app-health-check" yaml:"enableAppHealthCheck"`
AppHealthPath string `arg:"app-health-check-path" yaml:"appHealthCheckPath"`
AppHealthInterval int `arg:"app-health-probe-interval" ifneq:"0" yaml:"appHealthProbeInterval"`
AppHealthTimeout int `arg:"app-health-probe-timeout" ifneq:"0" yaml:"appHealthProbeTimeout"`
AppHealthThreshold int `arg:"app-health-threshold" ifneq:"0" yaml:"appHealthThreshold"`
EnableAPILogging bool `arg:"enable-api-logging" yaml:"enableApiLogging"`
DaprdInstallPath string `yaml:"runtimePath"`
Env map[string]string `yaml:"env"`
ConfigFile string `arg:"config" yaml:"configFilePath"`
AppProtocol string `arg:"app-protocol" yaml:"appProtocol" default:"http"`
APIListenAddresses string `arg:"dapr-listen-addresses" yaml:"apiListenAddresses"`
EnableProfiling bool `arg:"enable-profiling" yaml:"enableProfiling"`
LogLevel string `arg:"log-level" yaml:"logLevel"`
MaxConcurrency int `arg:"app-max-concurrency" yaml:"appMaxConcurrency" default:"-1"`
PlacementHostAddr string `arg:"placement-host-address" yaml:"placementHostAddress"`
ComponentsPath string `arg:"components-path"` // Deprecated in run template file: use ResourcesPaths instead.
ResourcesPath string `yaml:"resourcesPath"` // Deprecated in run template file: use ResourcesPaths instead.
ResourcesPaths []string `arg:"resources-path" yaml:"resourcesPaths"`
AppSSL bool `arg:"app-ssl" yaml:"appSSL"`
MaxRequestBodySize int `arg:"dapr-http-max-request-size" yaml:"daprHTTPMaxRequestSize" default:"-1"`
HTTPReadBufferSize int `arg:"dapr-http-read-buffer-size" yaml:"daprHTTPReadBufferSize" default:"-1"`
EnableAppHealth bool `arg:"enable-app-health-check" yaml:"enableAppHealthCheck"`
AppHealthPath string `arg:"app-health-check-path" yaml:"appHealthCheckPath"`
AppHealthInterval int `arg:"app-health-probe-interval" ifneq:"0" yaml:"appHealthProbeInterval"`
AppHealthTimeout int `arg:"app-health-probe-timeout" ifneq:"0" yaml:"appHealthProbeTimeout"`
AppHealthThreshold int `arg:"app-health-threshold" ifneq:"0" yaml:"appHealthThreshold"`
EnableAPILogging bool `arg:"enable-api-logging" yaml:"enableApiLogging"`
DaprdInstallPath string `yaml:"runtimePath"`
Env map[string]string `yaml:"env"`
DaprdLogDestination LogDestType `yaml:"daprdLogDestination"`
AppLogDestination LogDestType `yaml:"appLogDestination"`
}

func (meta *DaprMeta) newAppID() string {
Expand Down Expand Up @@ -412,3 +422,15 @@ func GetAppCommand(config *RunConfig) *exec.Cmd {

return cmd
}

func (l LogDestType) String() string {
return string(l)
}

func (l LogDestType) IsValid() error {
switch l {
case Console, File, FileAndConsole:
return nil
}
return fmt.Errorf("invalid log destination type: %s", l)
}
27 changes: 2 additions & 25 deletions pkg/standalone/runfileconfig/run_file_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ limitations under the License.
package runfileconfig

import (
"fmt"
"io"
"os"
"path/filepath"
Expand All @@ -23,15 +22,7 @@ import (
"github.com/dapr/cli/pkg/standalone"
)

type LogDestType string

const (
Console LogDestType = "console"
File LogDestType = "file"
FileAndConsole LogDestType = "fileAndConsole"
DefaultDaprdLogDest = File
DefaultAppLogDest = FileAndConsole

appLogFileNamePrefix = "app"
daprdLogFileNamePrefix = "daprd"
logFileExtension = ".log"
Expand All @@ -55,8 +46,6 @@ type App struct {
DaprdLogFileName string
AppLogWriteCloser io.WriteCloser
DaprdLogWriteCloser io.WriteCloser
DaprdLogDestination LogDestType `yaml:"daprdLogDestination"`
AppLogDestination LogDestType `yaml:"appLogDestination"`
}

// Common represents the configuration options for the common section in the run file.
Expand All @@ -75,7 +64,7 @@ func (a *App) GetLogsDir() string {
func (a *App) CreateAppLogFile() error {
var err error
var f *os.File
if a.AppLogDestination == Console {
if a.AppLogDestination == standalone.Console {
f = os.Stdout
} else {
f, err = a.createLogFile(appLogFileNamePrefix)
Expand All @@ -92,7 +81,7 @@ func (a *App) CreateAppLogFile() error {
func (a *App) CreateDaprdLogFile() error {
var err error
var f *os.File
if a.DaprdLogDestination == Console {
if a.DaprdLogDestination == standalone.Console {
f = os.Stdout
} else {
f, err = a.createLogFile(daprdLogFileNamePrefix)
Expand Down Expand Up @@ -126,15 +115,3 @@ func (a *App) CloseDaprdLogFile() error {
}
return nil
}

func (l LogDestType) String() string {
return string(l)
}

func (l LogDestType) IsValid() error {
switch l {
case Console, File, FileAndConsole:
return nil
}
return fmt.Errorf("invalid log destination type: %s", l)
}
4 changes: 2 additions & 2 deletions pkg/standalone/runfileconfig/run_file_config_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,12 +180,12 @@ func (a *RunFileConfig) setAppIDIfEmpty(app *App) error {
// It also validates the log destination if provided.
func (a *RunFileConfig) setAndValidateLogDestination(app *App) error {
if app.DaprdLogDestination == "" {
app.DaprdLogDestination = DefaultDaprdLogDest
app.DaprdLogDestination = standalone.DefaultDaprdLogDest
} else if err := app.DaprdLogDestination.IsValid(); err != nil {
return err
}
if app.AppLogDestination == "" {
app.AppLogDestination = DefaultAppLogDest
app.AppLogDestination = standalone.DefaultAppLogDest
} else if err := app.AppLogDestination.IsValid(); err != nil {
return err
}
Expand Down

0 comments on commit c65d816

Please sign in to comment.