Commit f4df374a9250b0ba7343ae349522a8fe276b448c

Authored by Georg Hopp
0 parents

Initial checkin

  1 +# Generated with m4
  2 +version.go
  3 +.version*
  1 +PACKAGE = gitlab.weird-web-workers.org/golang/logger
  2 +
  3 +SOURCES = version.go \
  4 + logger.go \
  5 + message.go
  6 +
  7 +VERSION = 0.0.1
  8 +REVISION = $(shell git rev-parse HEAD)
  9 +BUILDTIME= "$(shell date -u +'%Y-%m-%dT%H:%M:%SZ')"
  10 +
  11 +GOOS = $(shell go env GOOS)
  12 +GOARCH = $(shell go env GOARCH)
  13 +
  14 +LIBRARY = $(GOPATH)/pkg/$(GOOS)_$(GOARCH)/$(PACKAGE).a
  15 +
  16 +.PHONY: all clean .version
  17 +
  18 +all: $(LIBRARY)
  19 +
  20 +$(LIBRARY): $(SOURCES)
  21 + go install $(PACKAGE)
  22 +
  23 +.version:
  24 + -@printf "%s\n%s\n%s" \
  25 + "$(VERSION)" "$(REVISION)" "$(BUILDTIME)" >$@.new
  26 + -@diff $@ $@.new >/dev/null 2>&1 && rm $@.new || mv $@.new $@
  27 +
  28 +version.go: version.go.m4 .version
  29 + -@m4 -Dm4_version=$(VERSION) \
  30 + -Dm4_revision=$(REVISION) \
  31 + -Dm4_build_time=$(BUILDTIME) \
  32 + $< >$@
  33 +
  34 +clean:
  35 + -@rm -f version.go 2>/dev/null
  36 + -@rm -f .version 2>/dev/null
  37 + -@rm -f $(LIBRARY) 2>/dev/null
  1 +/*
  2 +Handle the applications logging needs/
  3 +
  4 +Authors:
  5 +Georg Hopp <georg@steffers.org>
  6 +
  7 +Changes:
  8 +2018-10-02 [Georg Hopp] File created.
  9 +
  10 +Copyright © 2018 Georg Hopp
  11 +
  12 +This program is free software: you can redistribute it and/or modify
  13 +it under the terms of the GNU General Public License as published by
  14 +the Free Software Foundation, either version 3 of the License, or
  15 +(at your option) any later version.
  16 +
  17 +This program is distributed in the hope that it will be useful,
  18 +but WITHOUT ANY WARRANTY; without even the implied warranty of
  19 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20 +GNU General Public License for more details.
  21 +
  22 +You should have received a copy of the GNU General Public License
  23 +along with this program. If not, see <http://www.gnu.org/licenses/>.
  24 +*/
  25 +package logger
  26 +
  27 +import (
  28 + "encoding/json"
  29 + "fmt"
  30 + "os"
  31 + "time"
  32 +)
  33 +
  34 +type Severity int
  35 +
  36 +const (
  37 + DEBUG Severity = iota
  38 + INFO
  39 + WARNING
  40 + ERROR
  41 + FATAL
  42 +)
  43 +
  44 +var (
  45 + SevStrings = [...]string{
  46 + "DEBUG",
  47 + "INFO",
  48 + "WARNING",
  49 + "ERROR",
  50 + "FATAL",
  51 + }
  52 +)
  53 +
  54 +type Logger struct {
  55 + logLevel Severity
  56 + name string
  57 +}
  58 +
  59 +var Default = GetLogger(INFO, "default")
  60 +
  61 +func GetLogger(sev Severity, name string) *Logger {
  62 + logger := Logger{sev, name}
  63 + return &logger
  64 +}
  65 +
  66 +func SevStringToSeverity(sevStr string) (sev Severity) {
  67 + switch sevStr {
  68 + case SevStrings[DEBUG]:
  69 + sev = DEBUG
  70 + case SevStrings[INFO]:
  71 + sev = INFO
  72 + case SevStrings[WARNING]:
  73 + sev = WARNING
  74 + case SevStrings[FATAL]:
  75 + sev = FATAL
  76 + case SevStrings[ERROR]:
  77 + fallthrough
  78 + default:
  79 + sev = ERROR
  80 + }
  81 + return
  82 +}
  83 +
  84 +func SeverityToSevString(sev Severity) string {
  85 + return SevStrings[sev]
  86 +}
  87 +
  88 +func (logger *Logger) SetLevel(level Severity) {
  89 + logger.logLevel = level
  90 +}
  91 +
  92 +func (logger *Logger) logToJson(
  93 + sev Severity, format string, args ...interface{}) string {
  94 +
  95 + message, err := json.Marshal(
  96 + logMessage{
  97 + time.Now().UTC().Format(time.RFC3339),
  98 + logger.name,
  99 + sev,
  100 + fmt.Sprintf(format, args...),
  101 + })
  102 + if err != nil {
  103 + // TODO Making it fatal if no logging can be done is
  104 + // kind of drastic
  105 + os.Exit(1)
  106 + }
  107 + return string(message[:])
  108 +}
  109 +
  110 +func (logger *Logger) log(sev Severity, format string, args ...interface{}) {
  111 +
  112 + var output *os.File
  113 +
  114 + if sev >= ERROR {
  115 + output = os.Stderr
  116 + } else {
  117 + output = os.Stdout
  118 + }
  119 +
  120 + if sev >= logger.logLevel {
  121 + fmt.Fprintln(output, logger.logToJson(sev, format, args...))
  122 + }
  123 +}
  124 +
  125 +func (logger *Logger) Debug(format string, args ...interface{}) {
  126 + logger.log(DEBUG, format, args...)
  127 +}
  128 +
  129 +func (logger *Logger) Info(format string, args ...interface{}) {
  130 + logger.log(INFO, format, args...)
  131 +}
  132 +
  133 +func (logger *Logger) Warning(format string, args ...interface{}) {
  134 + logger.log(WARNING, format, args...)
  135 +}
  136 +
  137 +func (logger *Logger) Error(format string, args ...interface{}) {
  138 + logger.log(ERROR, format, args...)
  139 +}
  140 +
  141 +func (logger *Logger) Panic(format string, args ...interface{}) {
  142 + logger.log(FATAL, format, args...)
  143 + panic(fmt.Sprintf(format, args...))
  144 +}
  145 +
  146 +func (logger *Logger) LogError(
  147 + err error, format string, args ...interface{}) {
  148 +
  149 + if err != nil {
  150 + logger.Error("%s: %s", fmt.Sprintf(format, args...), err)
  151 + }
  152 +}
  153 +
  154 +func (logger *Logger) FailOnError(
  155 + err error, format string, args ...interface{}) {
  156 +
  157 + if err != nil {
  158 + logger.Panic("%s: %s", fmt.Sprintf(format, args...), err)
  159 + }
  160 +}
  161 +
  162 +// vim: ts=4 sts=4 sw=4 noet tw=72:
  1 +/*
  2 +Handle the applications logging needs/
  3 +
  4 +Authors:
  5 +Georg Hopp <georg@steffers.org>
  6 +
  7 +Changes:
  8 +2018-10-02 [Georg Hopp] File created.
  9 +
  10 +Copyright © 2018 Georg Hopp
  11 +
  12 +This program is free software: you can redistribute it and/or modify
  13 +it under the terms of the GNU General Public License as published by
  14 +the Free Software Foundation, either version 3 of the License, or
  15 +(at your option) any later version.
  16 +
  17 +This program is distributed in the hope that it will be useful,
  18 +but WITHOUT ANY WARRANTY; without even the implied warranty of
  19 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20 +GNU General Public License for more details.
  21 +
  22 +You should have received a copy of the GNU General Public License
  23 +along with this program. If not, see <http://www.gnu.org/licenses/>.
  24 +*/
  25 +package logger
  26 +
  27 +import (
  28 + "fmt"
  29 +)
  30 +
  31 +type logMessage struct {
  32 + Time string `json:"time"`
  33 + Logger string `json:"logger"`
  34 + Severity Severity `json:"severity"`
  35 + Message string `json:"message"`
  36 +}
  37 +
  38 +func (sev Severity) MarshalJSON() (data []byte, err error) {
  39 + if int(sev) > len(SevStrings)-1 {
  40 + data = nil
  41 + err = fmt.Errorf("Unable to marshal unknown severity: %d", sev)
  42 + } else {
  43 + data = []byte("\"" + SevStrings[sev] + "\"")
  44 + err = nil
  45 + }
  46 + return
  47 +}
  48 +
  49 +// vim: ts=4 sts=4 sw=4 noet tw=72:
  1 +/*
  2 +Package version information.
  3 +
  4 +Authors:
  5 +Georg Hopp <georg@steffers.org>
  6 +
  7 +Changes:
  8 +2018-10-02 [Georg Hopp] File created.
  9 +
  10 +Copyright © 2018 Georg Hopp
  11 +
  12 +This program is free software: you can redistribute it and/or modify
  13 +it under the terms of the GNU General Public License as published by
  14 +the Free Software Foundation, either version 3 of the License, or
  15 +(at your option) any later version.
  16 +
  17 +This program is distributed in the hope that it will be useful,
  18 +but WITHOUT ANY WARRANTY; without even the implied warranty of
  19 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20 +GNU General Public License for more details.
  21 +
  22 +You should have received a copy of the GNU General Public License
  23 +along with this program. If not, see <http://www.gnu.org/licenses/>.
  24 +*/
  25 +package test
  26 +
  27 +const (
  28 + VERSION = "m4_version"
  29 + REVISION = "m4_revision"
  30 + BUILD_TIME = "m4_build_time"
  31 +)
  32 +
  33 +// vim: ts=4 sts=4 sw=4 noet tw=72:
Please register or login to post a comment