mirror of
https://github.com/safing/portbase
synced 2025-09-05 12:09:58 +00:00
Add logging metrics
This commit is contained in:
parent
a509febd48
commit
b304e88e79
3 changed files with 86 additions and 0 deletions
30
log/input.go
30
log/input.go
|
@ -8,6 +8,12 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
warnLogLines = new(uint64)
|
||||||
|
errLogLines = new(uint64)
|
||||||
|
critLogLines = new(uint64)
|
||||||
|
)
|
||||||
|
|
||||||
func log(level Severity, msg string, tracer *ContextTracer) {
|
func log(level Severity, msg string, tracer *ContextTracer) {
|
||||||
|
|
||||||
if !started.IsSet() {
|
if !started.IsSet() {
|
||||||
|
@ -146,6 +152,7 @@ func Infof(format string, things ...interface{}) {
|
||||||
|
|
||||||
// Warning is used to log (potentially) bad events, but nothing broke (even a little) and there is no need to panic yet.
|
// Warning is used to log (potentially) bad events, but nothing broke (even a little) and there is no need to panic yet.
|
||||||
func Warning(msg string) {
|
func Warning(msg string) {
|
||||||
|
atomic.AddUint64(warnLogLines, 1)
|
||||||
if fastcheck(WarningLevel) {
|
if fastcheck(WarningLevel) {
|
||||||
log(WarningLevel, msg, nil)
|
log(WarningLevel, msg, nil)
|
||||||
}
|
}
|
||||||
|
@ -153,6 +160,7 @@ func Warning(msg string) {
|
||||||
|
|
||||||
// Warningf is used to log (potentially) bad events, but nothing broke (even a little) and there is no need to panic yet.
|
// Warningf is used to log (potentially) bad events, but nothing broke (even a little) and there is no need to panic yet.
|
||||||
func Warningf(format string, things ...interface{}) {
|
func Warningf(format string, things ...interface{}) {
|
||||||
|
atomic.AddUint64(warnLogLines, 1)
|
||||||
if fastcheck(WarningLevel) {
|
if fastcheck(WarningLevel) {
|
||||||
log(WarningLevel, fmt.Sprintf(format, things...), nil)
|
log(WarningLevel, fmt.Sprintf(format, things...), nil)
|
||||||
}
|
}
|
||||||
|
@ -160,6 +168,7 @@ func Warningf(format string, things ...interface{}) {
|
||||||
|
|
||||||
// Error is used to log errors that break or impair functionality. The task/process may have to be aborted and tried again later. The system is still operational. Maybe User/Admin should be informed.
|
// Error is used to log errors that break or impair functionality. The task/process may have to be aborted and tried again later. The system is still operational. Maybe User/Admin should be informed.
|
||||||
func Error(msg string) {
|
func Error(msg string) {
|
||||||
|
atomic.AddUint64(errLogLines, 1)
|
||||||
if fastcheck(ErrorLevel) {
|
if fastcheck(ErrorLevel) {
|
||||||
log(ErrorLevel, msg, nil)
|
log(ErrorLevel, msg, nil)
|
||||||
}
|
}
|
||||||
|
@ -167,6 +176,7 @@ func Error(msg string) {
|
||||||
|
|
||||||
// Errorf is used to log errors that break or impair functionality. The task/process may have to be aborted and tried again later. The system is still operational.
|
// Errorf is used to log errors that break or impair functionality. The task/process may have to be aborted and tried again later. The system is still operational.
|
||||||
func Errorf(format string, things ...interface{}) {
|
func Errorf(format string, things ...interface{}) {
|
||||||
|
atomic.AddUint64(errLogLines, 1)
|
||||||
if fastcheck(ErrorLevel) {
|
if fastcheck(ErrorLevel) {
|
||||||
log(ErrorLevel, fmt.Sprintf(format, things...), nil)
|
log(ErrorLevel, fmt.Sprintf(format, things...), nil)
|
||||||
}
|
}
|
||||||
|
@ -174,6 +184,7 @@ func Errorf(format string, things ...interface{}) {
|
||||||
|
|
||||||
// Critical is used to log events that completely break the system. Operation connot continue. User/Admin must be informed.
|
// Critical is used to log events that completely break the system. Operation connot continue. User/Admin must be informed.
|
||||||
func Critical(msg string) {
|
func Critical(msg string) {
|
||||||
|
atomic.AddUint64(critLogLines, 1)
|
||||||
if fastcheck(CriticalLevel) {
|
if fastcheck(CriticalLevel) {
|
||||||
log(CriticalLevel, msg, nil)
|
log(CriticalLevel, msg, nil)
|
||||||
}
|
}
|
||||||
|
@ -181,7 +192,26 @@ func Critical(msg string) {
|
||||||
|
|
||||||
// Criticalf is used to log events that completely break the system. Operation connot continue. User/Admin must be informed.
|
// Criticalf is used to log events that completely break the system. Operation connot continue. User/Admin must be informed.
|
||||||
func Criticalf(format string, things ...interface{}) {
|
func Criticalf(format string, things ...interface{}) {
|
||||||
|
atomic.AddUint64(critLogLines, 1)
|
||||||
if fastcheck(CriticalLevel) {
|
if fastcheck(CriticalLevel) {
|
||||||
log(CriticalLevel, fmt.Sprintf(format, things...), nil)
|
log(CriticalLevel, fmt.Sprintf(format, things...), nil)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TotalWarningLogLines returns the total amount of warning log lines since
|
||||||
|
// start of the program.
|
||||||
|
func TotalWarningLogLines() uint64 {
|
||||||
|
return atomic.LoadUint64(warnLogLines)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TotalErrorLogLines returns the total amount of error log lines since start
|
||||||
|
// of the program.
|
||||||
|
func TotalErrorLogLines() uint64 {
|
||||||
|
return atomic.LoadUint64(errLogLines)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TotalCriticalLogLines returns the total amount of critical log lines since
|
||||||
|
// start of the program.
|
||||||
|
func TotalCriticalLogLines() uint64 {
|
||||||
|
return atomic.LoadUint64(critLogLines)
|
||||||
|
}
|
||||||
|
|
49
metrics/metric_logs.go
Normal file
49
metrics/metric_logs.go
Normal file
|
@ -0,0 +1,49 @@
|
||||||
|
package metrics
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/safing/portbase/api"
|
||||||
|
"github.com/safing/portbase/log"
|
||||||
|
)
|
||||||
|
|
||||||
|
func registeLogMetrics() (err error) {
|
||||||
|
_, err = NewFetchingCounter(
|
||||||
|
"logs/warning/total",
|
||||||
|
nil,
|
||||||
|
log.TotalWarningLogLines,
|
||||||
|
&Options{
|
||||||
|
Name: "Total Warning Log Lines",
|
||||||
|
Permission: api.PermitUser,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = NewFetchingCounter(
|
||||||
|
"logs/error/total",
|
||||||
|
nil,
|
||||||
|
log.TotalErrorLogLines,
|
||||||
|
&Options{
|
||||||
|
Name: "Total Error Log Lines",
|
||||||
|
Permission: api.PermitUser,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = NewFetchingCounter(
|
||||||
|
"logs/critical/total",
|
||||||
|
nil,
|
||||||
|
log.TotalCriticalLogLines,
|
||||||
|
&Options{
|
||||||
|
Name: "Total Critical Log Lines",
|
||||||
|
Permission: api.PermitUser,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
|
@ -29,6 +29,9 @@ var (
|
||||||
|
|
||||||
// ErrAlreadySet is returned when a value is already set and cannot be changed.
|
// ErrAlreadySet is returned when a value is already set and cannot be changed.
|
||||||
ErrAlreadySet = errors.New("already set")
|
ErrAlreadySet = errors.New("already set")
|
||||||
|
|
||||||
|
// ErrInvalidOptions is returned when invalid options where provided.
|
||||||
|
ErrInvalidOptions = errors.New("invalid options")
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
@ -59,6 +62,10 @@ func start() error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := registeLogMetrics(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
if err := registerAPI(); err != nil {
|
if err := registerAPI(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue