2014-12-08 20:53:00 +03:00
|
|
|
// +build linux darwin
|
|
|
|
|
2014-08-13 20:28:28 +04:00
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
//
|
|
|
|
// Contributor: Julien Vehent jvehent@mozilla.com [:ulfr]
|
2015-08-27 17:41:13 +03:00
|
|
|
|
2015-08-26 21:15:40 +03:00
|
|
|
package mig /* import "mig.ninja/mig" */
|
Scheduler: Happy New Year, with a massive refactoring commit.
The logic is pretty much the same as before, but this commit is a massive code
cleanup. There is now:
* proper error handling: each function uses panic internally, with a defer block
that transform the panic into an error, and eventually logs
* syslog, log levels, asynchronous logging into LOG channel, and mig.LOG type
* configuration file, loaded into global Context, accessible to all functions
* Context initialization, with connection to database, broker, creation of channels
and other wonders is handled in context.go
* split of the scheduler code into several files, more work to do on that front,
particularly for the management of the Flow in flow.go
* Action and Command are split out in their own files, with specific methods
That's about it. Did I mention Happy New Year?
2014-01-07 00:59:27 +04:00
|
|
|
|
2014-02-05 22:53:26 +04:00
|
|
|
import (
|
Scheduler: Happy New Year, with a massive refactoring commit.
The logic is pretty much the same as before, but this commit is a massive code
cleanup. There is now:
* proper error handling: each function uses panic internally, with a defer block
that transform the panic into an error, and eventually logs
* syslog, log levels, asynchronous logging into LOG channel, and mig.LOG type
* configuration file, loaded into global Context, accessible to all functions
* Context initialization, with connection to database, broker, creation of channels
and other wonders is handled in context.go
* split of the scheduler code into several files, more work to do on that front,
particularly for the management of the Flow in flow.go
* Action and Command are split out in their own files, with specific methods
That's about it. Did I mention Happy New Year?
2014-01-07 00:59:27 +04:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"log/syslog"
|
|
|
|
"os"
|
2016-04-03 21:19:13 +03:00
|
|
|
"sync"
|
Scheduler: Happy New Year, with a massive refactoring commit.
The logic is pretty much the same as before, but this commit is a massive code
cleanup. There is now:
* proper error handling: each function uses panic internally, with a defer block
that transform the panic into an error, and eventually logs
* syslog, log levels, asynchronous logging into LOG channel, and mig.LOG type
* configuration file, loaded into global Context, accessible to all functions
* Context initialization, with connection to database, broker, creation of channels
and other wonders is handled in context.go
* split of the scheduler code into several files, more work to do on that front,
particularly for the management of the Flow in flow.go
* Action and Command are split out in their own files, with specific methods
That's about it. Did I mention Happy New Year?
2014-01-07 00:59:27 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
MODE_STDOUT = 1 << iota
|
|
|
|
MODE_FILE
|
|
|
|
MODE_SYSLOG
|
|
|
|
)
|
|
|
|
|
|
|
|
// Logging stores the attributes needed to perform the logging
|
|
|
|
type Logging struct {
|
|
|
|
// configuration
|
|
|
|
Mode, Level, File, Host, Protocol, Facility string
|
2014-02-05 22:53:26 +04:00
|
|
|
Port int
|
2016-04-03 21:19:13 +03:00
|
|
|
MaxFileSize int64
|
Scheduler: Happy New Year, with a massive refactoring commit.
The logic is pretty much the same as before, but this commit is a massive code
cleanup. There is now:
* proper error handling: each function uses panic internally, with a defer block
that transform the panic into an error, and eventually logs
* syslog, log levels, asynchronous logging into LOG channel, and mig.LOG type
* configuration file, loaded into global Context, accessible to all functions
* Context initialization, with connection to database, broker, creation of channels
and other wonders is handled in context.go
* split of the scheduler code into several files, more work to do on that front,
particularly for the management of the Flow in flow.go
* Action and Command are split out in their own files, with specific methods
That's about it. Did I mention Happy New Year?
2014-01-07 00:59:27 +04:00
|
|
|
// internal
|
2016-04-03 21:19:13 +03:00
|
|
|
logmode int
|
|
|
|
maxlvl syslog.Priority
|
|
|
|
syslogfd *syslog.Writer
|
|
|
|
rotateWriter rotateLogWriter
|
Scheduler: Happy New Year, with a massive refactoring commit.
The logic is pretty much the same as before, but this commit is a massive code
cleanup. There is now:
* proper error handling: each function uses panic internally, with a defer block
that transform the panic into an error, and eventually logs
* syslog, log levels, asynchronous logging into LOG channel, and mig.LOG type
* configuration file, loaded into global Context, accessible to all functions
* Context initialization, with connection to database, broker, creation of channels
and other wonders is handled in context.go
* split of the scheduler code into several files, more work to do on that front,
particularly for the management of the Flow in flow.go
* Action and Command are split out in their own files, with specific methods
That's about it. Did I mention Happy New Year?
2014-01-07 00:59:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Log defines a log entry
|
|
|
|
type Log struct {
|
2014-06-17 18:59:46 +04:00
|
|
|
OpID, ActionID, CommandID float64
|
2014-02-05 22:53:26 +04:00
|
|
|
Sev, Desc string
|
|
|
|
Priority syslog.Priority
|
Scheduler: Happy New Year, with a massive refactoring commit.
The logic is pretty much the same as before, but this commit is a massive code
cleanup. There is now:
* proper error handling: each function uses panic internally, with a defer block
that transform the panic into an error, and eventually logs
* syslog, log levels, asynchronous logging into LOG channel, and mig.LOG type
* configuration file, loaded into global Context, accessible to all functions
* Context initialization, with connection to database, broker, creation of channels
and other wonders is handled in context.go
* split of the scheduler code into several files, more work to do on that front,
particularly for the management of the Flow in flow.go
* Action and Command are split out in their own files, with specific methods
That's about it. Did I mention Happy New Year?
2014-01-07 00:59:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l Log) Emerg() (mlog Log) {
|
|
|
|
mlog = l
|
|
|
|
mlog.Priority = syslog.LOG_EMERG
|
|
|
|
mlog.Sev = "emergency"
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l Log) Alert() (mlog Log) {
|
|
|
|
mlog = l
|
|
|
|
mlog.Priority = syslog.LOG_ALERT
|
|
|
|
mlog.Sev = "alert"
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l Log) Crit() (mlog Log) {
|
|
|
|
mlog = l
|
|
|
|
mlog.Priority = syslog.LOG_CRIT
|
|
|
|
mlog.Sev = "critical"
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l Log) Err() (mlog Log) {
|
|
|
|
mlog = l
|
|
|
|
mlog.Priority = syslog.LOG_ERR
|
|
|
|
mlog.Sev = "error"
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l Log) Warning() (mlog Log) {
|
|
|
|
mlog = l
|
|
|
|
mlog.Priority = syslog.LOG_WARNING
|
|
|
|
mlog.Sev = "warning"
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l Log) Notice() (mlog Log) {
|
|
|
|
mlog = l
|
|
|
|
mlog.Priority = syslog.LOG_NOTICE
|
|
|
|
mlog.Sev = "notice"
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l Log) Info() (mlog Log) {
|
|
|
|
mlog = l
|
|
|
|
mlog.Priority = syslog.LOG_INFO
|
|
|
|
mlog.Sev = "info"
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l Log) Debug() (mlog Log) {
|
|
|
|
mlog = l
|
|
|
|
mlog.Priority = syslog.LOG_DEBUG
|
|
|
|
mlog.Sev = "debug"
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-04-03 21:19:13 +03:00
|
|
|
// Custom type to satisfy io.Writer to use as file logging output, handles
|
|
|
|
// log file rotation
|
|
|
|
type rotateLogWriter struct {
|
|
|
|
sync.Mutex
|
|
|
|
filename string
|
|
|
|
fd *os.File
|
|
|
|
maxBytes int64 // Maximum size for log file, 0 means no rotation
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *rotateLogWriter) new(filename string, maxbytes int64) error {
|
|
|
|
r.filename = filename
|
|
|
|
r.maxBytes = maxbytes
|
|
|
|
return r.initAndCheck()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *rotateLogWriter) Write(output []byte) (int, error) {
|
|
|
|
var err error
|
|
|
|
err = r.initAndCheck()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return r.fd.Write(output)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *rotateLogWriter) initAndCheck() (err error) {
|
|
|
|
defer func() {
|
|
|
|
r.Unlock()
|
|
|
|
if e := recover(); e != nil {
|
|
|
|
err = fmt.Errorf("initAndCheck() -> %v", e)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
r.Lock()
|
|
|
|
// If we haven't initialized yet, open the log file
|
|
|
|
if r.fd == nil {
|
|
|
|
r.fd, err = os.OpenFile(r.filename, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0640)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// If the maximum file size is set to 0 we will never rotate
|
|
|
|
if r.maxBytes == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// Check the size of the existing log file, and rotate it if required.
|
|
|
|
// We only keep the current log file and one older one.
|
|
|
|
var fi os.FileInfo
|
|
|
|
fi, err = r.fd.Stat()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if fi.Size() < r.maxBytes {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Rotate the log and reinitialize it
|
|
|
|
// If the old file already exists remove it
|
|
|
|
rotatefile := r.filename + ".1"
|
|
|
|
_, err = os.Stat(rotatefile)
|
|
|
|
if err == nil || !os.IsNotExist(err) {
|
|
|
|
err = os.Remove(rotatefile)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Rename existing log file
|
|
|
|
err = os.Rename(r.filename, rotatefile)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
// Close existing descriptor and reinitialize
|
|
|
|
r.fd.Close()
|
|
|
|
r.fd, err = os.OpenFile(r.filename, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0640)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
Scheduler: Happy New Year, with a massive refactoring commit.
The logic is pretty much the same as before, but this commit is a massive code
cleanup. There is now:
* proper error handling: each function uses panic internally, with a defer block
that transform the panic into an error, and eventually logs
* syslog, log levels, asynchronous logging into LOG channel, and mig.LOG type
* configuration file, loaded into global Context, accessible to all functions
* Context initialization, with connection to database, broker, creation of channels
and other wonders is handled in context.go
* split of the scheduler code into several files, more work to do on that front,
particularly for the management of the Flow in flow.go
* Action and Command are split out in their own files, with specific methods
That's about it. Did I mention Happy New Year?
2014-01-07 00:59:27 +04:00
|
|
|
// InitLogger prepares the context for logging based on the configuration
|
|
|
|
// in Logging
|
2014-06-10 01:28:43 +04:00
|
|
|
func InitLogger(orig_logctx Logging, progname string) (logctx Logging, err error) {
|
Scheduler: Happy New Year, with a massive refactoring commit.
The logic is pretty much the same as before, but this commit is a massive code
cleanup. There is now:
* proper error handling: each function uses panic internally, with a defer block
that transform the panic into an error, and eventually logs
* syslog, log levels, asynchronous logging into LOG channel, and mig.LOG type
* configuration file, loaded into global Context, accessible to all functions
* Context initialization, with connection to database, broker, creation of channels
and other wonders is handled in context.go
* split of the scheduler code into several files, more work to do on that front,
particularly for the management of the Flow in flow.go
* Action and Command are split out in their own files, with specific methods
That's about it. Did I mention Happy New Year?
2014-01-07 00:59:27 +04:00
|
|
|
defer func() {
|
|
|
|
if e := recover(); e != nil {
|
|
|
|
err = fmt.Errorf("mig.InitLogger() -> %v", e)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
logctx = orig_logctx
|
|
|
|
switch logctx.Mode {
|
|
|
|
case "stdout":
|
|
|
|
logctx.logmode = MODE_STDOUT
|
|
|
|
logctx, err = initLogStdOut(logctx)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
case "file":
|
|
|
|
logctx.logmode = MODE_FILE
|
|
|
|
logctx, err = initLogFile(logctx)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
case "syslog":
|
|
|
|
logctx.logmode = MODE_SYSLOG
|
2014-06-10 01:28:43 +04:00
|
|
|
logctx, err = initSyslog(logctx, progname)
|
Scheduler: Happy New Year, with a massive refactoring commit.
The logic is pretty much the same as before, but this commit is a massive code
cleanup. There is now:
* proper error handling: each function uses panic internally, with a defer block
that transform the panic into an error, and eventually logs
* syslog, log levels, asynchronous logging into LOG channel, and mig.LOG type
* configuration file, loaded into global Context, accessible to all functions
* Context initialization, with connection to database, broker, creation of channels
and other wonders is handled in context.go
* split of the scheduler code into several files, more work to do on that front,
particularly for the management of the Flow in flow.go
* Action and Command are split out in their own files, with specific methods
That's about it. Did I mention Happy New Year?
2014-01-07 00:59:27 +04:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
log.Println("Logging mode is missing. Assuming stdout.")
|
|
|
|
logctx.Mode = "stdout"
|
|
|
|
logctx.logmode = MODE_STDOUT
|
|
|
|
logctx, err = initLogStdOut(logctx)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// set the minimal log level
|
|
|
|
switch logctx.Level {
|
|
|
|
case "emerg":
|
|
|
|
logctx.maxlvl = syslog.LOG_EMERG
|
|
|
|
case "alert":
|
|
|
|
logctx.maxlvl = syslog.LOG_ALERT
|
|
|
|
case "crit":
|
|
|
|
logctx.maxlvl = syslog.LOG_CRIT
|
|
|
|
case "err":
|
|
|
|
logctx.maxlvl = syslog.LOG_ERR
|
|
|
|
case "warning":
|
|
|
|
logctx.maxlvl = syslog.LOG_WARNING
|
|
|
|
case "notice":
|
|
|
|
logctx.maxlvl = syslog.LOG_NOTICE
|
|
|
|
case "info":
|
|
|
|
logctx.maxlvl = syslog.LOG_INFO
|
|
|
|
case "debug":
|
|
|
|
logctx.maxlvl = syslog.LOG_DEBUG
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// initSyslog creates a connection to syslog and stores the handler in ctx
|
2014-06-10 01:28:43 +04:00
|
|
|
func initSyslog(orig_logctx Logging, progname string) (logctx Logging, err error) {
|
Scheduler: Happy New Year, with a massive refactoring commit.
The logic is pretty much the same as before, but this commit is a massive code
cleanup. There is now:
* proper error handling: each function uses panic internally, with a defer block
that transform the panic into an error, and eventually logs
* syslog, log levels, asynchronous logging into LOG channel, and mig.LOG type
* configuration file, loaded into global Context, accessible to all functions
* Context initialization, with connection to database, broker, creation of channels
and other wonders is handled in context.go
* split of the scheduler code into several files, more work to do on that front,
particularly for the management of the Flow in flow.go
* Action and Command are split out in their own files, with specific methods
That's about it. Did I mention Happy New Year?
2014-01-07 00:59:27 +04:00
|
|
|
defer func() {
|
|
|
|
if e := recover(); e != nil {
|
|
|
|
err = fmt.Errorf("mig.initSyslog() -> %v", e)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
logctx = orig_logctx
|
|
|
|
if logctx.Host == "" {
|
|
|
|
panic("Syslog host is missing")
|
|
|
|
}
|
|
|
|
if logctx.Port < 1 {
|
|
|
|
panic("Syslog port is missing")
|
|
|
|
}
|
|
|
|
if logctx.Protocol == "" {
|
|
|
|
panic("Syslog protocol is missing")
|
|
|
|
}
|
|
|
|
dialaddr := logctx.Host + ":" + fmt.Sprintf("%d", logctx.Port)
|
2014-06-28 01:28:57 +04:00
|
|
|
logctx.syslogfd, err = syslog.Dial(logctx.Protocol, dialaddr, syslog.LOG_DAEMON|syslog.LOG_INFO, progname)
|
Scheduler: Happy New Year, with a massive refactoring commit.
The logic is pretty much the same as before, but this commit is a massive code
cleanup. There is now:
* proper error handling: each function uses panic internally, with a defer block
that transform the panic into an error, and eventually logs
* syslog, log levels, asynchronous logging into LOG channel, and mig.LOG type
* configuration file, loaded into global Context, accessible to all functions
* Context initialization, with connection to database, broker, creation of channels
and other wonders is handled in context.go
* split of the scheduler code into several files, more work to do on that front,
particularly for the management of the Flow in flow.go
* Action and Command are split out in their own files, with specific methods
That's about it. Did I mention Happy New Year?
2014-01-07 00:59:27 +04:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// initLogFile creates a logfile and stores the descriptor in ctx
|
2014-02-05 22:53:26 +04:00
|
|
|
func initLogFile(orig_logctx Logging) (logctx Logging, err error) {
|
Scheduler: Happy New Year, with a massive refactoring commit.
The logic is pretty much the same as before, but this commit is a massive code
cleanup. There is now:
* proper error handling: each function uses panic internally, with a defer block
that transform the panic into an error, and eventually logs
* syslog, log levels, asynchronous logging into LOG channel, and mig.LOG type
* configuration file, loaded into global Context, accessible to all functions
* Context initialization, with connection to database, broker, creation of channels
and other wonders is handled in context.go
* split of the scheduler code into several files, more work to do on that front,
particularly for the management of the Flow in flow.go
* Action and Command are split out in their own files, with specific methods
That's about it. Did I mention Happy New Year?
2014-01-07 00:59:27 +04:00
|
|
|
defer func() {
|
|
|
|
if e := recover(); e != nil {
|
|
|
|
err = fmt.Errorf("mig.InitLogFile() -> %v", e)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
logctx = orig_logctx
|
2016-04-03 21:19:13 +03:00
|
|
|
err = logctx.rotateWriter.new(logctx.File, logctx.MaxFileSize)
|
Scheduler: Happy New Year, with a massive refactoring commit.
The logic is pretty much the same as before, but this commit is a massive code
cleanup. There is now:
* proper error handling: each function uses panic internally, with a defer block
that transform the panic into an error, and eventually logs
* syslog, log levels, asynchronous logging into LOG channel, and mig.LOG type
* configuration file, loaded into global Context, accessible to all functions
* Context initialization, with connection to database, broker, creation of channels
and other wonders is handled in context.go
* split of the scheduler code into several files, more work to do on that front,
particularly for the management of the Flow in flow.go
* Action and Command are split out in their own files, with specific methods
That's about it. Did I mention Happy New Year?
2014-01-07 00:59:27 +04:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2016-04-03 21:19:13 +03:00
|
|
|
log.SetOutput(&logctx.rotateWriter)
|
Scheduler: Happy New Year, with a massive refactoring commit.
The logic is pretty much the same as before, but this commit is a massive code
cleanup. There is now:
* proper error handling: each function uses panic internally, with a defer block
that transform the panic into an error, and eventually logs
* syslog, log levels, asynchronous logging into LOG channel, and mig.LOG type
* configuration file, loaded into global Context, accessible to all functions
* Context initialization, with connection to database, broker, creation of channels
and other wonders is handled in context.go
* split of the scheduler code into several files, more work to do on that front,
particularly for the management of the Flow in flow.go
* Action and Command are split out in their own files, with specific methods
That's about it. Did I mention Happy New Year?
2014-01-07 00:59:27 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// initLogStdOut does nothing except storing in ctx that logs should be
|
|
|
|
// sent to stdout directly
|
2014-02-05 22:53:26 +04:00
|
|
|
func initLogStdOut(orig_logctx Logging) (logctx Logging, err error) {
|
Scheduler: Happy New Year, with a massive refactoring commit.
The logic is pretty much the same as before, but this commit is a massive code
cleanup. There is now:
* proper error handling: each function uses panic internally, with a defer block
that transform the panic into an error, and eventually logs
* syslog, log levels, asynchronous logging into LOG channel, and mig.LOG type
* configuration file, loaded into global Context, accessible to all functions
* Context initialization, with connection to database, broker, creation of channels
and other wonders is handled in context.go
* split of the scheduler code into several files, more work to do on that front,
particularly for the management of the Flow in flow.go
* Action and Command are split out in their own files, with specific methods
That's about it. Did I mention Happy New Year?
2014-01-07 00:59:27 +04:00
|
|
|
logctx = orig_logctx
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// processLog receives events and perform logging and evaluationg of the log
|
|
|
|
// if the log is too critical, Analyze will trigger a scheduler shutdown
|
|
|
|
func ProcessLog(logctx Logging, l Log) (stop bool, err error) {
|
|
|
|
stop = false
|
|
|
|
defer func() {
|
|
|
|
if e := recover(); e != nil {
|
|
|
|
err = fmt.Errorf("mig.ProcessLog() -> %v", e)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
var logline string
|
|
|
|
|
|
|
|
// if priority isn't set, use the default "info"
|
|
|
|
if l.Priority < 1 {
|
|
|
|
l.Priority = syslog.LOG_INFO
|
|
|
|
}
|
|
|
|
|
|
|
|
// discard logs that have a priority that's higher than the
|
|
|
|
// minimal llogel we are configured to log
|
|
|
|
if l.Priority > logctx.maxlvl {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if l.OpID > 0 {
|
2014-06-17 23:44:53 +04:00
|
|
|
logline += fmt.Sprintf("%.0f ", l.OpID)
|
Scheduler: Happy New Year, with a massive refactoring commit.
The logic is pretty much the same as before, but this commit is a massive code
cleanup. There is now:
* proper error handling: each function uses panic internally, with a defer block
that transform the panic into an error, and eventually logs
* syslog, log levels, asynchronous logging into LOG channel, and mig.LOG type
* configuration file, loaded into global Context, accessible to all functions
* Context initialization, with connection to database, broker, creation of channels
and other wonders is handled in context.go
* split of the scheduler code into several files, more work to do on that front,
particularly for the management of the Flow in flow.go
* Action and Command are split out in their own files, with specific methods
That's about it. Did I mention Happy New Year?
2014-01-07 00:59:27 +04:00
|
|
|
} else {
|
|
|
|
logline += "- "
|
|
|
|
}
|
|
|
|
|
|
|
|
if l.ActionID > 0 {
|
2014-06-17 23:44:53 +04:00
|
|
|
logline += fmt.Sprintf("%.0f ", l.ActionID)
|
Scheduler: Happy New Year, with a massive refactoring commit.
The logic is pretty much the same as before, but this commit is a massive code
cleanup. There is now:
* proper error handling: each function uses panic internally, with a defer block
that transform the panic into an error, and eventually logs
* syslog, log levels, asynchronous logging into LOG channel, and mig.LOG type
* configuration file, loaded into global Context, accessible to all functions
* Context initialization, with connection to database, broker, creation of channels
and other wonders is handled in context.go
* split of the scheduler code into several files, more work to do on that front,
particularly for the management of the Flow in flow.go
* Action and Command are split out in their own files, with specific methods
That's about it. Did I mention Happy New Year?
2014-01-07 00:59:27 +04:00
|
|
|
} else {
|
|
|
|
logline += "- "
|
|
|
|
}
|
|
|
|
|
|
|
|
if l.CommandID > 0 {
|
2014-06-17 23:44:53 +04:00
|
|
|
logline += fmt.Sprintf("%.0f ", l.CommandID)
|
Scheduler: Happy New Year, with a massive refactoring commit.
The logic is pretty much the same as before, but this commit is a massive code
cleanup. There is now:
* proper error handling: each function uses panic internally, with a defer block
that transform the panic into an error, and eventually logs
* syslog, log levels, asynchronous logging into LOG channel, and mig.LOG type
* configuration file, loaded into global Context, accessible to all functions
* Context initialization, with connection to database, broker, creation of channels
and other wonders is handled in context.go
* split of the scheduler code into several files, more work to do on that front,
particularly for the management of the Flow in flow.go
* Action and Command are split out in their own files, with specific methods
That's about it. Did I mention Happy New Year?
2014-01-07 00:59:27 +04:00
|
|
|
} else {
|
|
|
|
logline += "- "
|
|
|
|
}
|
|
|
|
|
|
|
|
if l.Sev != "" {
|
|
|
|
logline += "[" + l.Sev + "] "
|
|
|
|
} else {
|
|
|
|
logline += "[info] "
|
|
|
|
}
|
|
|
|
|
|
|
|
if l.Desc != "" {
|
|
|
|
logline += l.Desc
|
|
|
|
} else {
|
|
|
|
err = fmt.Errorf("Missing mandatory description in logent")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch logctx.logmode {
|
2014-02-05 22:53:26 +04:00
|
|
|
case MODE_SYSLOG:
|
Scheduler: Happy New Year, with a massive refactoring commit.
The logic is pretty much the same as before, but this commit is a massive code
cleanup. There is now:
* proper error handling: each function uses panic internally, with a defer block
that transform the panic into an error, and eventually logs
* syslog, log levels, asynchronous logging into LOG channel, and mig.LOG type
* configuration file, loaded into global Context, accessible to all functions
* Context initialization, with connection to database, broker, creation of channels
and other wonders is handled in context.go
* split of the scheduler code into several files, more work to do on that front,
particularly for the management of the Flow in flow.go
* Action and Command are split out in their own files, with specific methods
That's about it. Did I mention Happy New Year?
2014-01-07 00:59:27 +04:00
|
|
|
switch l.Priority {
|
|
|
|
// emergency logging causes the scheduler to shut down
|
|
|
|
case syslog.LOG_EMERG:
|
|
|
|
err = logctx.syslogfd.Emerg(logline)
|
|
|
|
stop = true
|
2014-06-27 04:32:46 +04:00
|
|
|
return
|
Scheduler: Happy New Year, with a massive refactoring commit.
The logic is pretty much the same as before, but this commit is a massive code
cleanup. There is now:
* proper error handling: each function uses panic internally, with a defer block
that transform the panic into an error, and eventually logs
* syslog, log levels, asynchronous logging into LOG channel, and mig.LOG type
* configuration file, loaded into global Context, accessible to all functions
* Context initialization, with connection to database, broker, creation of channels
and other wonders is handled in context.go
* split of the scheduler code into several files, more work to do on that front,
particularly for the management of the Flow in flow.go
* Action and Command are split out in their own files, with specific methods
That's about it. Did I mention Happy New Year?
2014-01-07 00:59:27 +04:00
|
|
|
case syslog.LOG_ALERT:
|
|
|
|
err = logctx.syslogfd.Alert(logline)
|
2014-06-27 04:32:46 +04:00
|
|
|
return
|
Scheduler: Happy New Year, with a massive refactoring commit.
The logic is pretty much the same as before, but this commit is a massive code
cleanup. There is now:
* proper error handling: each function uses panic internally, with a defer block
that transform the panic into an error, and eventually logs
* syslog, log levels, asynchronous logging into LOG channel, and mig.LOG type
* configuration file, loaded into global Context, accessible to all functions
* Context initialization, with connection to database, broker, creation of channels
and other wonders is handled in context.go
* split of the scheduler code into several files, more work to do on that front,
particularly for the management of the Flow in flow.go
* Action and Command are split out in their own files, with specific methods
That's about it. Did I mention Happy New Year?
2014-01-07 00:59:27 +04:00
|
|
|
case syslog.LOG_CRIT:
|
|
|
|
err = logctx.syslogfd.Crit(logline)
|
2014-06-27 04:32:46 +04:00
|
|
|
return
|
Scheduler: Happy New Year, with a massive refactoring commit.
The logic is pretty much the same as before, but this commit is a massive code
cleanup. There is now:
* proper error handling: each function uses panic internally, with a defer block
that transform the panic into an error, and eventually logs
* syslog, log levels, asynchronous logging into LOG channel, and mig.LOG type
* configuration file, loaded into global Context, accessible to all functions
* Context initialization, with connection to database, broker, creation of channels
and other wonders is handled in context.go
* split of the scheduler code into several files, more work to do on that front,
particularly for the management of the Flow in flow.go
* Action and Command are split out in their own files, with specific methods
That's about it. Did I mention Happy New Year?
2014-01-07 00:59:27 +04:00
|
|
|
case syslog.LOG_ERR:
|
|
|
|
err = logctx.syslogfd.Err(logline)
|
2014-06-27 04:32:46 +04:00
|
|
|
return
|
Scheduler: Happy New Year, with a massive refactoring commit.
The logic is pretty much the same as before, but this commit is a massive code
cleanup. There is now:
* proper error handling: each function uses panic internally, with a defer block
that transform the panic into an error, and eventually logs
* syslog, log levels, asynchronous logging into LOG channel, and mig.LOG type
* configuration file, loaded into global Context, accessible to all functions
* Context initialization, with connection to database, broker, creation of channels
and other wonders is handled in context.go
* split of the scheduler code into several files, more work to do on that front,
particularly for the management of the Flow in flow.go
* Action and Command are split out in their own files, with specific methods
That's about it. Did I mention Happy New Year?
2014-01-07 00:59:27 +04:00
|
|
|
case syslog.LOG_WARNING:
|
|
|
|
err = logctx.syslogfd.Warning(logline)
|
2014-06-27 04:32:46 +04:00
|
|
|
return
|
Scheduler: Happy New Year, with a massive refactoring commit.
The logic is pretty much the same as before, but this commit is a massive code
cleanup. There is now:
* proper error handling: each function uses panic internally, with a defer block
that transform the panic into an error, and eventually logs
* syslog, log levels, asynchronous logging into LOG channel, and mig.LOG type
* configuration file, loaded into global Context, accessible to all functions
* Context initialization, with connection to database, broker, creation of channels
and other wonders is handled in context.go
* split of the scheduler code into several files, more work to do on that front,
particularly for the management of the Flow in flow.go
* Action and Command are split out in their own files, with specific methods
That's about it. Did I mention Happy New Year?
2014-01-07 00:59:27 +04:00
|
|
|
case syslog.LOG_NOTICE:
|
|
|
|
err = logctx.syslogfd.Notice(logline)
|
2014-06-27 04:32:46 +04:00
|
|
|
return
|
Scheduler: Happy New Year, with a massive refactoring commit.
The logic is pretty much the same as before, but this commit is a massive code
cleanup. There is now:
* proper error handling: each function uses panic internally, with a defer block
that transform the panic into an error, and eventually logs
* syslog, log levels, asynchronous logging into LOG channel, and mig.LOG type
* configuration file, loaded into global Context, accessible to all functions
* Context initialization, with connection to database, broker, creation of channels
and other wonders is handled in context.go
* split of the scheduler code into several files, more work to do on that front,
particularly for the management of the Flow in flow.go
* Action and Command are split out in their own files, with specific methods
That's about it. Did I mention Happy New Year?
2014-01-07 00:59:27 +04:00
|
|
|
case syslog.LOG_INFO:
|
|
|
|
err = logctx.syslogfd.Info(logline)
|
2014-06-27 04:32:46 +04:00
|
|
|
return
|
Scheduler: Happy New Year, with a massive refactoring commit.
The logic is pretty much the same as before, but this commit is a massive code
cleanup. There is now:
* proper error handling: each function uses panic internally, with a defer block
that transform the panic into an error, and eventually logs
* syslog, log levels, asynchronous logging into LOG channel, and mig.LOG type
* configuration file, loaded into global Context, accessible to all functions
* Context initialization, with connection to database, broker, creation of channels
and other wonders is handled in context.go
* split of the scheduler code into several files, more work to do on that front,
particularly for the management of the Flow in flow.go
* Action and Command are split out in their own files, with specific methods
That's about it. Did I mention Happy New Year?
2014-01-07 00:59:27 +04:00
|
|
|
case syslog.LOG_DEBUG:
|
|
|
|
err = logctx.syslogfd.Debug(logline)
|
2014-06-27 04:32:46 +04:00
|
|
|
return
|
Scheduler: Happy New Year, with a massive refactoring commit.
The logic is pretty much the same as before, but this commit is a massive code
cleanup. There is now:
* proper error handling: each function uses panic internally, with a defer block
that transform the panic into an error, and eventually logs
* syslog, log levels, asynchronous logging into LOG channel, and mig.LOG type
* configuration file, loaded into global Context, accessible to all functions
* Context initialization, with connection to database, broker, creation of channels
and other wonders is handled in context.go
* split of the scheduler code into several files, more work to do on that front,
particularly for the management of the Flow in flow.go
* Action and Command are split out in their own files, with specific methods
That's about it. Did I mention Happy New Year?
2014-01-07 00:59:27 +04:00
|
|
|
default:
|
|
|
|
err = logctx.syslogfd.Info(logline)
|
2014-06-27 04:32:46 +04:00
|
|
|
return
|
Scheduler: Happy New Year, with a massive refactoring commit.
The logic is pretty much the same as before, but this commit is a massive code
cleanup. There is now:
* proper error handling: each function uses panic internally, with a defer block
that transform the panic into an error, and eventually logs
* syslog, log levels, asynchronous logging into LOG channel, and mig.LOG type
* configuration file, loaded into global Context, accessible to all functions
* Context initialization, with connection to database, broker, creation of channels
and other wonders is handled in context.go
* split of the scheduler code into several files, more work to do on that front,
particularly for the management of the Flow in flow.go
* Action and Command are split out in their own files, with specific methods
That's about it. Did I mention Happy New Year?
2014-01-07 00:59:27 +04:00
|
|
|
}
|
|
|
|
case MODE_STDOUT:
|
|
|
|
log.Println(logline)
|
2014-06-27 04:32:46 +04:00
|
|
|
return
|
Scheduler: Happy New Year, with a massive refactoring commit.
The logic is pretty much the same as before, but this commit is a massive code
cleanup. There is now:
* proper error handling: each function uses panic internally, with a defer block
that transform the panic into an error, and eventually logs
* syslog, log levels, asynchronous logging into LOG channel, and mig.LOG type
* configuration file, loaded into global Context, accessible to all functions
* Context initialization, with connection to database, broker, creation of channels
and other wonders is handled in context.go
* split of the scheduler code into several files, more work to do on that front,
particularly for the management of the Flow in flow.go
* Action and Command are split out in their own files, with specific methods
That's about it. Did I mention Happy New Year?
2014-01-07 00:59:27 +04:00
|
|
|
case MODE_FILE:
|
|
|
|
log.Println(logline)
|
2014-06-27 04:32:46 +04:00
|
|
|
return
|
Scheduler: Happy New Year, with a massive refactoring commit.
The logic is pretty much the same as before, but this commit is a massive code
cleanup. There is now:
* proper error handling: each function uses panic internally, with a defer block
that transform the panic into an error, and eventually logs
* syslog, log levels, asynchronous logging into LOG channel, and mig.LOG type
* configuration file, loaded into global Context, accessible to all functions
* Context initialization, with connection to database, broker, creation of channels
and other wonders is handled in context.go
* split of the scheduler code into several files, more work to do on that front,
particularly for the management of the Flow in flow.go
* Action and Command are split out in their own files, with specific methods
That's about it. Did I mention Happy New Year?
2014-01-07 00:59:27 +04:00
|
|
|
default:
|
|
|
|
log.Println(logline)
|
2014-06-27 04:32:46 +04:00
|
|
|
return
|
Scheduler: Happy New Year, with a massive refactoring commit.
The logic is pretty much the same as before, but this commit is a massive code
cleanup. There is now:
* proper error handling: each function uses panic internally, with a defer block
that transform the panic into an error, and eventually logs
* syslog, log levels, asynchronous logging into LOG channel, and mig.LOG type
* configuration file, loaded into global Context, accessible to all functions
* Context initialization, with connection to database, broker, creation of channels
and other wonders is handled in context.go
* split of the scheduler code into several files, more work to do on that front,
particularly for the management of the Flow in flow.go
* Action and Command are split out in their own files, with specific methods
That's about it. Did I mention Happy New Year?
2014-01-07 00:59:27 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (logctx Logging) Destroy() {
|
|
|
|
if logctx.Mode == "syslog" {
|
|
|
|
logctx.syslogfd.Close()
|
|
|
|
}
|
|
|
|
}
|