Skip to content

Commit 1ad9a54

Browse files
authored
Don't require env var PROJECT_NAME (#34)
* Don't require env var PROJECT_NAME * log
1 parent 0e5eb61 commit 1ad9a54

File tree

6 files changed

+35
-32
lines changed

6 files changed

+35
-32
lines changed

log/init.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
package log
22

33
import (
4-
golog "log"
5-
64
"github.com/replicatedcom/saaskit/param"
75
)
86

97
func Init(logOpts *LogOptions, mailLogOpts *MailLogOptions, slackLogOpts *SlackLogOptions) {
10-
projectName := param.Lookup("PROJECT_NAME", "", false)
11-
if projectName == "" {
12-
golog.Fatalf("PROJECT_NAME must be set prior to configuring the saaskit logger")
13-
}
148
InitLog(logOpts)
159
InitMail(mailLogOpts)
1610
InitSlack(slackLogOpts)
11+
12+
if param.Lookup("PROJECT_NAME", "", false) == "" {
13+
Infof("Environment variable PROJECT_NAME not set")
14+
}
1715
}

log/log.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,15 @@ func InitLog(opts *LogOptions) {
5555
}
5656

5757
if opts.BugsnagKey != "" {
58-
bugsnag.Configure(bugsnag.Configuration{
58+
config := bugsnag.Configuration{
5959
ReleaseStage: param.Lookup("ENVIRONMENT", "/replicated/environment", false),
6060
APIKey: opts.BugsnagKey,
6161
NotifyReleaseStages: []string{"production", "staging"},
62-
ProjectPackages: []string{fmt.Sprintf("%s*", param.Lookup("PROJECT_NAME", "", false))},
63-
})
62+
}
63+
if projectName := param.Lookup("PROJECT_NAME", "", false); projectName != "" {
64+
config.ProjectPackages = append(config.ProjectPackages, fmt.Sprintf("%s*", projectName))
65+
}
66+
bugsnag.Configure(config)
6467

6568
hook, err := NewBugsnagHook()
6669
if err != nil {
@@ -129,7 +132,7 @@ func Errorf(format string, args ...interface{}) {
129132

130133
func shortPath(pathIn string) string {
131134
projectName := param.Lookup("PROJECT_NAME", "", false)
132-
if !strings.Contains(pathIn, projectName) {
135+
if projectName == "" || !strings.Contains(pathIn, projectName) {
133136
return pathIn
134137
}
135138

log/mail.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@ func InitMail(opts *MailLogOptions) {
2525
}
2626

2727
MailLog.OnBeforeLog(func(entry *logrus.Entry) *logrus.Entry {
28-
return entry.WithFields(
29-
logrus.Fields{
30-
"project.name": param.Lookup("PROJECT_NAME", "", false),
31-
"environment": param.Lookup("ENVIRONMENT", "/replicated/environment", false),
32-
},
33-
)
28+
fields := logrus.Fields{
29+
"environment": param.Lookup("ENVIRONMENT", "/replicated/environment", false),
30+
}
31+
if projectName := param.Lookup("PROJECT_NAME", "", false); projectName != "" {
32+
fields["project.name"] = projectName
33+
}
34+
return entry.WithFields(fields)
3435
})
3536

3637
if opts.Recipients != "" {

log/mailapi_hook.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,15 @@ func (hook *MailAPIHook) Fire(entry *logrus.Entry) error {
2929
}
3030

3131
context := map[string]interface{}{
32-
"project_name": hook.ProjectName,
33-
"time": entry.Time.Format(mailLoggerTimeFormat),
34-
"message": entry.Message,
35-
"fields": entry.Data,
32+
"time": entry.Time.Format(mailLoggerTimeFormat),
33+
"message": entry.Message,
34+
"fields": entry.Data,
3635
}
37-
38-
if err := mail.SendMailInternal("", "", recipients, "internal-log-message", subject, context); err != nil {
39-
return err
36+
if hook.ProjectName != "" {
37+
context["project_name"] = hook.ProjectName
4038
}
4139

42-
return nil
40+
return mail.SendMailInternal("", "", recipients, "internal-log-message", subject, context)
4341
}
4442

4543
func (sh *MailAPIHook) Levels() []logrus.Level {

log/slack.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@ func InitSlack(opts *SlackLogOptions) {
2525
}
2626

2727
SlackLog.OnBeforeLog(func(entry *logrus.Entry) *logrus.Entry {
28-
return entry.WithFields(
29-
logrus.Fields{
30-
"project.name": param.Lookup("PROJECT_NAME", "", false),
31-
"environment": param.Lookup("ENVIRONMENT", "/replicated/environment", false),
32-
},
33-
)
28+
fields := logrus.Fields{
29+
"environment": param.Lookup("ENVIRONMENT", "/replicated/environment", false),
30+
}
31+
if projectName := param.Lookup("PROJECT_NAME", "", false); projectName != "" {
32+
fields["project.name"] = projectName
33+
}
34+
return entry.WithFields(fields)
3435
})
3536

3637
slackLogHookURL := param.Lookup("SLACKLOG_HOOK_URL", "/slack/hook_url", true)

param/param.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,15 @@ func (c *ParamCache) ssmGet(ssmName string, decrypt bool) (string, bool) {
6969
log.Printf("Failed to get ssm param %s: %v", ssmName, err)
7070
return "", false
7171
}
72-
if len(resp.InvalidParameters) > 0 {
72+
if os.Getenv("DEBUG") != "" && len(resp.InvalidParameters) > 0 {
7373
for _, p := range resp.InvalidParameters {
7474
log.Printf("Ssm param %s invalid", *p)
7575
}
76-
return "", false
7776
}
7877

78+
if len(resp.Parameters) == 0 {
79+
return "", false
80+
}
7981
val := *(resp.Parameters[0].Value)
8082
c.mapSet(ssmName, val)
8183
return val, true

0 commit comments

Comments
 (0)