Skip to content

Commit 52988e3

Browse files
authored
Merge pull request #3324 from semaphoreui/syslog
syslog
2 parents 6142ae7 + c65f493 commit 52988e3

File tree

3 files changed

+38
-6
lines changed

3 files changed

+38
-6
lines changed

cli/cmd/root.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ package cmd
22

33
import (
44
"fmt"
5+
"log/syslog"
56
"net/http"
67
"net/url"
78
"os"
89
"strings"
910

1011
"github.com/semaphoreui/semaphore/api/helpers"
1112
"github.com/semaphoreui/semaphore/services/server"
13+
lSyslog "github.com/sirupsen/logrus/hooks/syslog"
1214

1315
"github.com/gorilla/handlers"
1416
"github.com/semaphoreui/semaphore/api"
@@ -71,8 +73,22 @@ func Execute() {
7173
}
7274
}
7375

76+
func initSyslog(conf *util.SyslogConfig) {
77+
if conf.Enabled {
78+
hook, err := lSyslog.NewSyslogHook(conf.Network, conf.Address, syslog.LOG_DEBUG, conf.Tag)
79+
if err == nil {
80+
log.AddHook(hook)
81+
} else {
82+
log.WithError(err).Fatal("Failed to create syslog hook")
83+
}
84+
}
85+
}
86+
7487
func runService() {
7588
store := createStore("root")
89+
90+
initSyslog(util.Config.Log.Channels.Syslog)
91+
7692
state := proTasks.NewTaskStateStore()
7793
terraformStore := proFactory.NewTerraformStore(store)
7894
ansibleTaskRepo := proFactory.NewAnsibleTaskRepository(store)

services/tasks/TaskPool.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ func (p *TaskPool) Run() {
170170

171171
db.StoreSession(p.store, "new task", func() {
172172
//p.Queue = append(p.Queue, task)
173-
msg := "Task " + strconv.Itoa(task.Task.ID) + " added to queue"
173+
msg := "Task " + getTaskName(task) + " added to queue"
174174
task.Log(msg)
175175
log.Info(msg)
176176
task.saveStatus()
@@ -184,6 +184,10 @@ func (p *TaskPool) Run() {
184184
}
185185
}
186186

187+
func getTaskName(t *TaskRunner) string {
188+
return t.Template.Name + " " + strconv.Itoa(t.Task.ID)
189+
}
190+
187191
func (p *TaskPool) handleQueue() {
188192
for t := range p.queueEvents {
189193
switch t.eventType {
@@ -208,7 +212,7 @@ func (p *TaskPool) handleQueue() {
208212
if curr.Task.Status == task_logger.TaskFailStatus {
209213
//delete failed TaskRunner from queue
210214
_ = p.state.DequeueAt(i)
211-
log.Info("Task " + strconv.Itoa(curr.Task.ID) + " removed from queue")
215+
log.Info("Task " + getTaskName(curr) + " removed from queue")
212216
continue
213217
}
214218

@@ -313,10 +317,10 @@ func (p *TaskPool) writeLogs(logs []logRecord) {
313317
}
314318

315319
func runTask(task *TaskRunner, p *TaskPool) {
316-
log.Info("Set resource locker with TaskRunner " + strconv.Itoa(task.Task.ID))
320+
log.Info("Set resource locker with TaskRunner " + getTaskName(task))
317321
p.onTaskRun(task)
318322

319-
log.Info("Task " + strconv.Itoa(task.Task.ID) + " started")
323+
log.Info("Task " + getTaskName(task) + " started")
320324
go func() {
321325
time.Sleep(1 * time.Second)
322326
task.run()

util/config.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,21 @@ type TaskLogType struct {
157157
ResultLogger *lumberjack.Logger `json:"result_logger,omitempty" env:"SEMAPHORE_TASK_RESULT_LOGGER"`
158158
}
159159

160+
type ConfigLogChannels struct {
161+
Syslog *SyslogConfig `json:"syslog,omitempty"`
162+
}
163+
160164
type ConfigLog struct {
161-
Events *EventLogType `json:"events,omitempty"`
162-
Tasks *TaskLogType `json:"tasks,omitempty"`
165+
Events *EventLogType `json:"events,omitempty"`
166+
Tasks *TaskLogType `json:"tasks,omitempty"`
167+
Channels *ConfigLogChannels `json:"channels,omitempty"`
168+
}
169+
170+
type SyslogConfig struct {
171+
Enabled bool `json:"enabled" env:"SEMAPHORE_SYSLOG_ENABLED"`
172+
Network string `json:"network,omitempty" env:"SEMAPHORE_SYSLOG_NETWORK"`
173+
Address string `json:"address,omitempty" env:"SEMAPHORE_SYSLOG_ADDRESS"`
174+
Tag string `json:"tag,omitempty" env:"SEMAPHORE_SYSLOG_TAG"`
163175
}
164176

165177
type ConfigProcess struct {

0 commit comments

Comments
 (0)