mirror of https://github.com/grafana/grafana
Conflicts: pkg/api/api.go pkg/setting/setting.gopull/5622/head
commit
0667d73660
@ -1,157 +0,0 @@ |
||||
// Copyright 2014 The Gogs Authors. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package log |
||||
|
||||
import ( |
||||
"encoding/json" |
||||
"fmt" |
||||
"log" |
||||
"os" |
||||
"runtime" |
||||
) |
||||
|
||||
type Brush func(string) string |
||||
|
||||
func NewBrush(color string) Brush { |
||||
pre := "\033[" |
||||
reset := "\033[0m" |
||||
return func(text string) string { |
||||
return pre + color + "m" + text + reset |
||||
} |
||||
} |
||||
|
||||
var ( |
||||
Red = NewBrush("1;31") |
||||
Purple = NewBrush("1;35") |
||||
Yellow = NewBrush("1;33") |
||||
Green = NewBrush("1;32") |
||||
Blue = NewBrush("1;34") |
||||
Cyan = NewBrush("1;36") |
||||
|
||||
colors = []Brush{ |
||||
Cyan, // Trace cyan
|
||||
Blue, // Debug blue
|
||||
Green, // Info green
|
||||
Yellow, // Warn yellow
|
||||
Red, // Error red
|
||||
Purple, // Critical purple
|
||||
Red, // Fatal red
|
||||
} |
||||
consoleWriter = &ConsoleWriter{lg: log.New(os.Stdout, "", 0), |
||||
Level: TRACE} |
||||
) |
||||
|
||||
// ConsoleWriter implements LoggerInterface and writes messages to terminal.
|
||||
type ConsoleWriter struct { |
||||
lg *log.Logger |
||||
Level LogLevel `json:"level"` |
||||
Formatting bool `json:"formatting"` |
||||
} |
||||
|
||||
// create ConsoleWriter returning as LoggerInterface.
|
||||
func NewConsole() LoggerInterface { |
||||
return &ConsoleWriter{ |
||||
lg: log.New(os.Stderr, "", log.Ldate|log.Ltime), |
||||
Level: TRACE, |
||||
Formatting: true, |
||||
} |
||||
} |
||||
|
||||
func (cw *ConsoleWriter) Init(config string) error { |
||||
return json.Unmarshal([]byte(config), cw) |
||||
} |
||||
|
||||
func (cw *ConsoleWriter) WriteMsg(msg string, skip int, level LogLevel) error { |
||||
if cw.Level > level { |
||||
return nil |
||||
} |
||||
if runtime.GOOS == "windows" || !cw.Formatting { |
||||
cw.lg.Println(msg) |
||||
} else { |
||||
cw.lg.Println(colors[level](msg)) |
||||
} |
||||
return nil |
||||
} |
||||
|
||||
func (_ *ConsoleWriter) Flush() { |
||||
|
||||
} |
||||
|
||||
func (_ *ConsoleWriter) Destroy() { |
||||
} |
||||
|
||||
func printConsole(level LogLevel, msg string) { |
||||
consoleWriter.WriteMsg(msg, 0, level) |
||||
} |
||||
|
||||
func printfConsole(level LogLevel, format string, v ...interface{}) { |
||||
consoleWriter.WriteMsg(fmt.Sprintf(format, v...), 0, level) |
||||
} |
||||
|
||||
// ConsoleTrace prints to stdout using TRACE colors
|
||||
func ConsoleTrace(s string) { |
||||
printConsole(TRACE, s) |
||||
} |
||||
|
||||
// ConsoleTracef prints a formatted string to stdout using TRACE colors
|
||||
func ConsoleTracef(format string, v ...interface{}) { |
||||
printfConsole(TRACE, format, v...) |
||||
} |
||||
|
||||
// ConsoleDebug prints to stdout using DEBUG colors
|
||||
func ConsoleDebug(s string) { |
||||
printConsole(DEBUG, s) |
||||
} |
||||
|
||||
// ConsoleDebugf prints a formatted string to stdout using DEBUG colors
|
||||
func ConsoleDebugf(format string, v ...interface{}) { |
||||
printfConsole(DEBUG, format, v...) |
||||
} |
||||
|
||||
// ConsoleInfo prints to stdout using INFO colors
|
||||
func ConsoleInfo(s string) { |
||||
printConsole(INFO, s) |
||||
} |
||||
|
||||
// ConsoleInfof prints a formatted string to stdout using INFO colors
|
||||
func ConsoleInfof(format string, v ...interface{}) { |
||||
printfConsole(INFO, format, v...) |
||||
} |
||||
|
||||
// ConsoleWarn prints to stdout using WARN colors
|
||||
func ConsoleWarn(s string) { |
||||
printConsole(WARN, s) |
||||
} |
||||
|
||||
// ConsoleWarnf prints a formatted string to stdout using WARN colors
|
||||
func ConsoleWarnf(format string, v ...interface{}) { |
||||
printfConsole(WARN, format, v...) |
||||
} |
||||
|
||||
// ConsoleError prints to stdout using ERROR colors
|
||||
func ConsoleError(s string) { |
||||
printConsole(ERROR, s) |
||||
} |
||||
|
||||
// ConsoleErrorf prints a formatted string to stdout using ERROR colors
|
||||
func ConsoleErrorf(format string, v ...interface{}) { |
||||
printfConsole(ERROR, format, v...) |
||||
} |
||||
|
||||
// ConsoleFatal prints to stdout using FATAL colors
|
||||
func ConsoleFatal(s string) { |
||||
printConsole(FATAL, s) |
||||
os.Exit(1) |
||||
} |
||||
|
||||
// ConsoleFatalf prints a formatted string to stdout using FATAL colors
|
||||
func ConsoleFatalf(format string, v ...interface{}) { |
||||
printfConsole(FATAL, format, v...) |
||||
os.Exit(1) |
||||
} |
||||
|
||||
func init() { |
||||
Register("console", NewConsole) |
||||
} |
||||
@ -0,0 +1,5 @@ |
||||
package log |
||||
|
||||
type DisposableHandler interface { |
||||
Close() |
||||
} |
||||
@ -0,0 +1,31 @@ |
||||
package log |
||||
|
||||
import "github.com/inconshreveable/log15" |
||||
|
||||
type Lvl int |
||||
|
||||
const ( |
||||
LvlCrit Lvl = iota |
||||
LvlError |
||||
LvlWarn |
||||
LvlInfo |
||||
LvlDebug |
||||
) |
||||
|
||||
type Logger interface { |
||||
// New returns a new Logger that has this logger's context plus the given context
|
||||
New(ctx ...interface{}) log15.Logger |
||||
|
||||
// GetHandler gets the handler associated with the logger.
|
||||
GetHandler() log15.Handler |
||||
|
||||
// SetHandler updates the logger to write records to the specified handler.
|
||||
SetHandler(h log15.Handler) |
||||
|
||||
// Log a message at the given level with context key/value pairs
|
||||
Debug(msg string, ctx ...interface{}) |
||||
Info(msg string, ctx ...interface{}) |
||||
Warn(msg string, ctx ...interface{}) |
||||
Error(msg string, ctx ...interface{}) |
||||
Crit(msg string, ctx ...interface{}) |
||||
} |
||||
@ -0,0 +1,174 @@ |
||||
// Copyright 2013 Martini Authors
|
||||
// Copyright 2014 The Macaron Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License"): you may
|
||||
// not use this file except in compliance with the License. You may obtain
|
||||
// a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
// License for the specific language governing permissions and limitations
|
||||
// under the License.
|
||||
|
||||
package middleware |
||||
|
||||
import ( |
||||
"bytes" |
||||
"fmt" |
||||
"io/ioutil" |
||||
"net/http" |
||||
"runtime" |
||||
|
||||
"gopkg.in/macaron.v1" |
||||
|
||||
"github.com/go-macaron/inject" |
||||
"github.com/grafana/grafana/pkg/log" |
||||
"github.com/grafana/grafana/pkg/setting" |
||||
) |
||||
|
||||
const ( |
||||
panicHtml = `<html> |
||||
<head><title>PANIC: %s</title> |
||||
<meta charset="utf-8" /> |
||||
<style type="text/css"> |
||||
html, body { |
||||
font-family: "Roboto", sans-serif; |
||||
color: #333333; |
||||
background-color: #ea5343; |
||||
margin: 0px; |
||||
} |
||||
h1 { |
||||
color: #d04526; |
||||
background-color: #ffffff; |
||||
padding: 20px; |
||||
border-bottom: 1px dashed #2b3848; |
||||
} |
||||
pre { |
||||
margin: 20px; |
||||
padding: 20px; |
||||
border: 2px solid #2b3848; |
||||
background-color: #ffffff; |
||||
white-space: pre-wrap; /* css-3 */ |
||||
white-space: -moz-pre-wrap; /* Mozilla, since 1999 */ |
||||
white-space: -pre-wrap; /* Opera 4-6 */ |
||||
white-space: -o-pre-wrap; /* Opera 7 */ |
||||
word-wrap: break-word; /* Internet Explorer 5.5+ */ |
||||
} |
||||
</style> |
||||
</head><body> |
||||
<h1>PANIC</h1> |
||||
<pre style="font-weight: bold;">%s</pre> |
||||
<pre>%s</pre> |
||||
</body> |
||||
</html>` |
||||
) |
||||
|
||||
var ( |
||||
dunno = []byte("???") |
||||
centerDot = []byte("·") |
||||
dot = []byte(".") |
||||
slash = []byte("/") |
||||
) |
||||
|
||||
// stack returns a nicely formated stack frame, skipping skip frames
|
||||
func stack(skip int) []byte { |
||||
buf := new(bytes.Buffer) // the returned data
|
||||
// As we loop, we open files and read them. These variables record the currently
|
||||
// loaded file.
|
||||
var lines [][]byte |
||||
var lastFile string |
||||
for i := skip; ; i++ { // Skip the expected number of frames
|
||||
pc, file, line, ok := runtime.Caller(i) |
||||
if !ok { |
||||
break |
||||
} |
||||
// Print this much at least. If we can't find the source, it won't show.
|
||||
fmt.Fprintf(buf, "%s:%d (0x%x)\n", file, line, pc) |
||||
if file != lastFile { |
||||
data, err := ioutil.ReadFile(file) |
||||
if err != nil { |
||||
continue |
||||
} |
||||
lines = bytes.Split(data, []byte{'\n'}) |
||||
lastFile = file |
||||
} |
||||
fmt.Fprintf(buf, "\t%s: %s\n", function(pc), source(lines, line)) |
||||
} |
||||
return buf.Bytes() |
||||
} |
||||
|
||||
// source returns a space-trimmed slice of the n'th line.
|
||||
func source(lines [][]byte, n int) []byte { |
||||
n-- // in stack trace, lines are 1-indexed but our array is 0-indexed
|
||||
if n < 0 || n >= len(lines) { |
||||
return dunno |
||||
} |
||||
return bytes.TrimSpace(lines[n]) |
||||
} |
||||
|
||||
// function returns, if possible, the name of the function containing the PC.
|
||||
func function(pc uintptr) []byte { |
||||
fn := runtime.FuncForPC(pc) |
||||
if fn == nil { |
||||
return dunno |
||||
} |
||||
name := []byte(fn.Name()) |
||||
// The name includes the path name to the package, which is unnecessary
|
||||
// since the file name is already included. Plus, it has center dots.
|
||||
// That is, we see
|
||||
// runtime/debug.*T·ptrmethod
|
||||
// and want
|
||||
// *T.ptrmethod
|
||||
// Also the package path might contains dot (e.g. code.google.com/...),
|
||||
// so first eliminate the path prefix
|
||||
if lastslash := bytes.LastIndex(name, slash); lastslash >= 0 { |
||||
name = name[lastslash+1:] |
||||
} |
||||
if period := bytes.Index(name, dot); period >= 0 { |
||||
name = name[period+1:] |
||||
} |
||||
name = bytes.Replace(name, centerDot, dot, -1) |
||||
return name |
||||
} |
||||
|
||||
// Recovery returns a middleware that recovers from any panics and writes a 500 if there was one.
|
||||
// While Martini is in development mode, Recovery will also output the panic as HTML.
|
||||
func Recovery() macaron.Handler { |
||||
return func(c *macaron.Context) { |
||||
defer func() { |
||||
if err := recover(); err != nil { |
||||
stack := stack(3) |
||||
|
||||
panicLogger := log.Root |
||||
// try to get request logger
|
||||
if ctx, ok := c.Data["ctx"]; ok { |
||||
ctxTyped := ctx.(*Context) |
||||
panicLogger = ctxTyped.Logger |
||||
} |
||||
|
||||
panicLogger.Error("Request error", "error", err, "stack", string(stack)) |
||||
|
||||
// Lookup the current responsewriter
|
||||
val := c.GetVal(inject.InterfaceOf((*http.ResponseWriter)(nil))) |
||||
res := val.Interface().(http.ResponseWriter) |
||||
|
||||
// respond with panic message while in development mode
|
||||
var body []byte |
||||
if setting.Env == setting.DEV { |
||||
res.Header().Set("Content-Type", "text/html") |
||||
body = []byte(fmt.Sprintf(panicHtml, err, err, stack)) |
||||
} |
||||
|
||||
res.WriteHeader(http.StatusInternalServerError) |
||||
if nil != body { |
||||
res.Write(body) |
||||
} |
||||
} |
||||
}() |
||||
|
||||
c.Next() |
||||
} |
||||
} |
||||
Loading…
Reference in new issue