mirror of
https://github.com/hanzoai/forge.git
synced 2026-10-30 15:00:39 +00:00
2978 files. The fork is 2026-07-15 (rebrand: gitea.dev -> github.com/hanzoai/git), so 2026 is the year, and "Copyright 2026 Hanzo AI, Inc." was already the form used by the handful of files that had ours. Shape is now: // Copyright 2026 Hanzo AI, Inc. All rights reserved. // Copyright 2015 The Gogs Authors. All rights reserved. // Copyright 2019 The Gitea Authors. All rights reserved. // SPDX-License-Identifier: MIT Ours on top, theirs retained verbatim. Retained because MIT requires it, and separately because they are true — those authors did write that code. Not renamed to "The Git Authors": Git is a different project under a different license, and putting its name here would attribute this code to people who have nothing to do with it. Adding our line is the fix; overwriting theirs would have been both a licence violation and a false statement. gofmt still reports import-order drift in some of these files. It predates this change (verified against a stash) and is left alone rather than burying a one-line-per-file legal change under 3000 files of unrelated reordering. Build clean. Co-authored-by: Hanzo Dev <[email protected]>
118 lines
2.0 KiB
Go
118 lines
2.0 KiB
Go
// Copyright 2026 Hanzo AI, Inc. All rights reserved.
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package log
|
|
|
|
import (
|
|
"bytes"
|
|
"strings"
|
|
|
|
"github.com/hanzoai/git/modules/json"
|
|
)
|
|
|
|
// Level is the level of the logger
|
|
type Level int
|
|
|
|
const (
|
|
UNDEFINED Level = iota
|
|
TRACE
|
|
DEBUG
|
|
INFO
|
|
WARN
|
|
ERROR
|
|
FATAL
|
|
NONE
|
|
)
|
|
|
|
const CRITICAL = ERROR // most logger frameworks doesn't support CRITICAL, and it doesn't seem useful
|
|
|
|
var toString = map[Level]string{
|
|
UNDEFINED: "undefined",
|
|
|
|
TRACE: "trace",
|
|
DEBUG: "debug",
|
|
INFO: "info",
|
|
WARN: "warn",
|
|
ERROR: "error",
|
|
|
|
FATAL: "fatal",
|
|
NONE: "none",
|
|
}
|
|
|
|
var toLevel = map[string]Level{
|
|
"undefined": UNDEFINED,
|
|
|
|
"trace": TRACE,
|
|
"debug": DEBUG,
|
|
"info": INFO,
|
|
"warn": WARN,
|
|
"warning": WARN,
|
|
"error": ERROR,
|
|
|
|
"fatal": FATAL,
|
|
"none": NONE,
|
|
}
|
|
|
|
var levelToColor = map[Level][]ColorAttribute{
|
|
TRACE: {Bold, FgCyan},
|
|
DEBUG: {Bold, FgBlue},
|
|
INFO: {Bold, FgGreen},
|
|
WARN: {Bold, FgYellow},
|
|
ERROR: {Bold, FgRed},
|
|
FATAL: {Bold, BgRed},
|
|
NONE: {Reset},
|
|
}
|
|
|
|
func (l Level) String() string {
|
|
s, ok := toString[l]
|
|
if ok {
|
|
return s
|
|
}
|
|
return "info"
|
|
}
|
|
|
|
func (l Level) ColorAttributes() []ColorAttribute {
|
|
color, ok := levelToColor[l]
|
|
if ok {
|
|
return color
|
|
}
|
|
none := levelToColor[NONE]
|
|
return none
|
|
}
|
|
|
|
// MarshalJSON takes a Level and turns it into text
|
|
func (l Level) MarshalJSON() ([]byte, error) {
|
|
buffer := bytes.NewBufferString(`"`)
|
|
buffer.WriteString(toString[l])
|
|
buffer.WriteString(`"`)
|
|
return buffer.Bytes(), nil
|
|
}
|
|
|
|
// UnmarshalJSON takes text and turns it into a Level
|
|
func (l *Level) UnmarshalJSON(b []byte) error {
|
|
var tmp any
|
|
err := json.Unmarshal(b, &tmp)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
switch v := tmp.(type) {
|
|
case string:
|
|
*l = LevelFromString(v)
|
|
case int:
|
|
*l = LevelFromString(Level(v).String())
|
|
default:
|
|
*l = INFO
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// LevelFromString takes a level string and returns a Level
|
|
func LevelFromString(level string) Level {
|
|
if l, ok := toLevel[strings.ToLower(level)]; ok {
|
|
return l
|
|
}
|
|
return INFO
|
|
}
|