As we reevaluate how to best support and maintain Staging Ref in the future, we encourage development teams using this environment to highlight their use cases in the following issue: https://gitlab.com/gitlab-com/gl-infra/software-delivery/framework/software-delivery-framework-issue-tracker/-/issues/36.

Skip to content
Snippets Groups Projects
Commit 946ae2ad authored by Nick Thomas's avatar Nick Thomas
Browse files

Merge branch 'sh-fix-log-permission-error' into 'main'

Fix gitlab-shell panic when log file not writable

See merge request gitlab-org/gitlab-shell!453
parents c0a75dae 48ec7c61
No related branches found
No related tags found
No related merge requests found
package logger
import (
"fmt"
"io/ioutil"
"log/syslog"
"os"
Loading
Loading
@@ -22,8 +23,14 @@ func Configure(cfg *config.Config) {
logFile, err := os.OpenFile(cfg.LogFile, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
if err != nil {
progName, _ := os.Executable()
syslogLogger, err := syslog.NewLogger(syslog.LOG_ERR|syslog.LOG_USER, 0)
syslogLogger.Print(progName + ": Unable to configure logging: " + err.Error())
syslogLogger, syslogLoggerErr := syslog.NewLogger(syslog.LOG_ERR|syslog.LOG_USER, 0)
if syslogLoggerErr == nil {
msg := fmt.Sprintf("%s: Unable to configure logging: %v\n", progName, err.Error())
syslogLogger.Print(msg)
} else {
msg := fmt.Sprintf("%s: Unable to configure logging: %v, %v\n", progName, err.Error(), syslogLoggerErr.Error())
fmt.Fprintf(os.Stderr, msg)
}
// Discard logs since a log file was specified but couldn't be opened
log.SetOutput(ioutil.Discard)
Loading
Loading
Loading
Loading
@@ -30,3 +30,17 @@ func TestConfigure(t *testing.T) {
require.NoError(t, err)
require.True(t, strings.Contains(string(data), `msg":"this is a test"`))
}
func TestConfigureWithPermissionError(t *testing.T) {
tmpPath, err := ioutil.TempDir(os.TempDir(), "logtest-")
require.NoError(t, err)
defer os.RemoveAll(tmpPath)
config := config.Config{
LogFile: tmpPath,
LogFormat: "json",
}
Configure(&config)
log.Info("this is a test")
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment