mirror of https://github.com/prometheus/prometheus
Signed-off-by: tariqibrahim <tariq.ibrahim@microsoft.com>pull/4927/head
commit
1fd438ed2b
@ -0,0 +1,21 @@
|
|||||||
|
MIT License
|
||||||
|
|
||||||
|
Copyright (c) 2018 Kubermatic
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
@ -0,0 +1,65 @@
|
|||||||
|
# glog-gokit
|
||||||
|
|
||||||
|
This packages is a replacement for [glog](github.com/golang/glog)
|
||||||
|
in projects that use the [go-kit logger](https://godoc.org/github.com/go-kit/kit/log).
|
||||||
|
|
||||||
|
It is inspired by istio's glog package for zap:
|
||||||
|
https://github.com/istio/glog
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
Override the official glog package with this one.
|
||||||
|
This simply replaces the code in `vendor/golang/glog` with the code of this package.
|
||||||
|
|
||||||
|
In your `Gopkg.toml`:
|
||||||
|
```toml
|
||||||
|
[[override]]
|
||||||
|
name = "github.com/golang/glog"
|
||||||
|
source = "github.com/kubermatic/glog-gokit"
|
||||||
|
```
|
||||||
|
|
||||||
|
In your `main.go`:
|
||||||
|
```go
|
||||||
|
// Import the package like it is original glog
|
||||||
|
import "github.com/golang/glog"
|
||||||
|
|
||||||
|
|
||||||
|
// Create go-kit logger in your main.go
|
||||||
|
logger := log.NewLogfmtLogger(log.NewSyncWriter(os.Stdout))
|
||||||
|
logger = log.With(logger, "ts", log.DefaultTimestampUTC)
|
||||||
|
logger = log.With(logger, "caller", log.DefaultCaller)
|
||||||
|
logger = level.NewFilter(logger, level.AllowAll())
|
||||||
|
|
||||||
|
// Overriding the default glog with our go-kit glog implementation.
|
||||||
|
// Thus we need to pass it our go-kit logger object.
|
||||||
|
glog.SetLogger(logger)
|
||||||
|
```
|
||||||
|
|
||||||
|
Setting the logger to the glog package **MUST** happen before using glog in any package.
|
||||||
|
|
||||||
|
## Function Levels
|
||||||
|
|
||||||
|
| glog | gokit |
|
||||||
|
| ------------ | ----- |
|
||||||
|
| Info | Debug |
|
||||||
|
| InfoDepth | Debug |
|
||||||
|
| Infof | Debug |
|
||||||
|
| Infoln | Debug |
|
||||||
|
| Warning | Warn |
|
||||||
|
| WarningDepth | Warn |
|
||||||
|
| Warningf | Warn |
|
||||||
|
| Warningln | Warn |
|
||||||
|
| Error | Error |
|
||||||
|
| ErrorDepth | Error |
|
||||||
|
| Errorf | Error |
|
||||||
|
| Errorln | Error |
|
||||||
|
| Exit | Error |
|
||||||
|
| ExitDepth | Error |
|
||||||
|
| Exitf | Error |
|
||||||
|
| Exitln | Error |
|
||||||
|
| Fatal | Error |
|
||||||
|
| FatalDepth | Error |
|
||||||
|
| Fatalf | Error |
|
||||||
|
| Fatalln | Error |
|
||||||
|
|
||||||
|
This table is rather opinionated and build for use with the Kubernetes' [Go client](https://github.com/kubernetes/client-go).
|
@ -0,0 +1,143 @@
|
|||||||
|
package glog
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"math"
|
||||||
|
"os"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"github.com/go-kit/kit/log"
|
||||||
|
"github.com/go-kit/kit/log/level"
|
||||||
|
)
|
||||||
|
|
||||||
|
var maxLevel Level = math.MaxInt32
|
||||||
|
var logger = log.NewNopLogger()
|
||||||
|
var mu = sync.Mutex{}
|
||||||
|
|
||||||
|
// SetLogger redirects glog logging to the given logger.
|
||||||
|
// It must be called prior any call to glog.
|
||||||
|
func SetLogger(l log.Logger) {
|
||||||
|
mu.Lock()
|
||||||
|
logger = l
|
||||||
|
mu.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
// ClampLevel clamps the leveled logging at the specified value.
|
||||||
|
// It must be called prior any call to glog.
|
||||||
|
func ClampLevel(l Level) {
|
||||||
|
mu.Lock()
|
||||||
|
maxLevel = l
|
||||||
|
mu.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
type Level int32
|
||||||
|
|
||||||
|
type Verbose bool
|
||||||
|
|
||||||
|
func V(level Level) Verbose { return level <= maxLevel }
|
||||||
|
|
||||||
|
func (v Verbose) Info(args ...interface{}) {
|
||||||
|
if v {
|
||||||
|
level.Debug(logger).Log("func", "Verbose.Info", "msg", fmt.Sprint(args...))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v Verbose) Infoln(args ...interface{}) {
|
||||||
|
if v {
|
||||||
|
level.Debug(logger).Log("func", "Verbose.Infoln", "msg", fmt.Sprint(args...))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v Verbose) Infof(format string, args ...interface{}) {
|
||||||
|
if v {
|
||||||
|
level.Debug(logger).Log("func", "Verbose.Infof", "msg", fmt.Sprintf(format, args...))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Info(args ...interface{}) {
|
||||||
|
level.Debug(logger).Log("func", "Info", "msg", fmt.Sprint(args...))
|
||||||
|
}
|
||||||
|
|
||||||
|
func InfoDepth(depth int, args ...interface{}) {
|
||||||
|
level.Debug(logger).Log("func", "InfoDepth", "msg", fmt.Sprint(args...))
|
||||||
|
}
|
||||||
|
|
||||||
|
func Infoln(args ...interface{}) {
|
||||||
|
level.Debug(logger).Log("func", "Infoln", "msg", fmt.Sprint(args...))
|
||||||
|
}
|
||||||
|
|
||||||
|
func Infof(format string, args ...interface{}) {
|
||||||
|
level.Debug(logger).Log("func", "Infof", "msg", fmt.Sprintf(format, args...))
|
||||||
|
}
|
||||||
|
|
||||||
|
func Warning(args ...interface{}) {
|
||||||
|
level.Warn(logger).Log("func", "Warning", "msg", fmt.Sprint(args...))
|
||||||
|
}
|
||||||
|
|
||||||
|
func WarningDepth(depth int, args ...interface{}) {
|
||||||
|
level.Warn(logger).Log("func", "WarningDepth", "msg", fmt.Sprint(args...))
|
||||||
|
}
|
||||||
|
|
||||||
|
func Warningln(args ...interface{}) {
|
||||||
|
level.Warn(logger).Log("func", "Warningln", "msg", fmt.Sprint(args...))
|
||||||
|
}
|
||||||
|
|
||||||
|
func Warningf(format string, args ...interface{}) {
|
||||||
|
level.Warn(logger).Log("func", "Warningf", "msg", fmt.Sprintf(format, args...))
|
||||||
|
}
|
||||||
|
|
||||||
|
func Error(args ...interface{}) {
|
||||||
|
level.Error(logger).Log("func", "Error", "msg", fmt.Sprint(args...))
|
||||||
|
}
|
||||||
|
|
||||||
|
func ErrorDepth(depth int, args ...interface{}) {
|
||||||
|
level.Error(logger).Log("func", "ErrorDepth", "msg", fmt.Sprint(args...))
|
||||||
|
}
|
||||||
|
|
||||||
|
func Errorln(args ...interface{}) {
|
||||||
|
level.Error(logger).Log("func", "Errorln", "msg", fmt.Sprint(args...))
|
||||||
|
}
|
||||||
|
|
||||||
|
func Errorf(format string, args ...interface{}) {
|
||||||
|
level.Error(logger).Log("func", "Errorf", "msg", fmt.Sprintf(format, args...))
|
||||||
|
}
|
||||||
|
|
||||||
|
func Fatal(args ...interface{}) {
|
||||||
|
level.Error(logger).Log("func", "Fatal", "msg", fmt.Sprint(args...))
|
||||||
|
os.Exit(255)
|
||||||
|
}
|
||||||
|
|
||||||
|
func FatalDepth(depth int, args ...interface{}) {
|
||||||
|
level.Error(logger).Log("func", "FatalDepth", "msg", fmt.Sprint(args...))
|
||||||
|
os.Exit(255)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Fatalln(args ...interface{}) {
|
||||||
|
level.Error(logger).Log("func", "Fatalln", "msg", fmt.Sprint(args...))
|
||||||
|
os.Exit(255)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Fatalf(format string, args ...interface{}) {
|
||||||
|
level.Error(logger).Log("func", "Fatalf", "msg", fmt.Sprintf(format, args...))
|
||||||
|
os.Exit(255)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Exit(args ...interface{}) {
|
||||||
|
level.Error(logger).Log("func", "Exit", "msg", fmt.Sprint(args...))
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExitDepth(depth int, args ...interface{}) {
|
||||||
|
level.Error(logger).Log("func", "ExitDepth", "msg", fmt.Sprint(args...))
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Exitln(args ...interface{}) {
|
||||||
|
level.Error(logger).Log("func", "Exitln", "msg", fmt.Sprint(args...))
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Exitf(format string, args ...interface{}) {
|
||||||
|
level.Error(logger).Log("func", "Exitf", "msg", fmt.Sprintf(format, args...))
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
Loading…
Reference in new issue