starlog/standed.go

785 lines
14 KiB
Go
Raw Normal View History

2020-12-21 17:22:45 +08:00
package starlog
import (
2026-03-19 16:37:57 +08:00
"context"
2020-12-22 11:07:20 +08:00
"fmt"
2020-12-21 17:22:45 +08:00
"io"
2026-03-19 16:37:57 +08:00
"log"
2020-12-21 17:22:45 +08:00
"math/rand"
"sync"
"time"
)
var Std *StarLogger
var stdmu sync.Mutex
func init() {
rand.Seed(time.Now().UnixNano())
Std = NewStarlog(nil)
}
func SetShowColor(val bool) {
Std.SetShowColor(val)
}
func GetShowColor() bool {
return Std.GetShowColor()
}
func SetLevelColor(level int, color []Attr) {
Std.SetLevelColor(level, color)
}
func GetLevelColor(level int) []Attr {
return Std.GetLevelColor(level)
}
2026-03-19 16:37:57 +08:00
func SetLevel(level int) {
Std.SetLevel(level)
}
func GetLevel() int {
return Std.GetLevel()
}
func IsLevelEnabled(level int) bool {
return Std.IsLevelEnabled(level)
}
func SetColorMode(mode ColorMode) {
Std.SetColorMode(mode)
}
func GetColorMode() ColorMode {
return Std.GetColorMode()
}
func SetKeywordColor(keyword string, color []Attr) {
Std.SetKeywordColor(keyword, color)
}
func RemoveKeywordColor(keyword string) {
Std.RemoveKeywordColor(keyword)
}
func SetKeywordColors(colors map[string][]Attr) {
Std.SetKeywordColors(colors)
}
func GetKeywordColors() map[string][]Attr {
return Std.GetKeywordColors()
}
func ClearKeywordColors() {
Std.ClearKeywordColors()
}
func SetKeywordMatchOptions(opts KeywordMatchOptions) {
Std.SetKeywordMatchOptions(opts)
}
func GetKeywordMatchOptions() KeywordMatchOptions {
return Std.GetKeywordMatchOptions()
}
func SetKeywordIgnoreCase(enable bool) {
Std.SetKeywordIgnoreCase(enable)
}
func GetKeywordIgnoreCase() bool {
return Std.GetKeywordIgnoreCase()
}
func SetKeywordWholeWord(enable bool) {
Std.SetKeywordWholeWord(enable)
}
func GetKeywordWholeWord() bool {
return Std.GetKeywordWholeWord()
}
func ApplyKeywordPreset(preset KeywordPreset) {
Std.ApplyKeywordPreset(preset)
}
func MergeKeywordPreset(preset KeywordPreset) {
Std.MergeKeywordPreset(preset)
}
func SetShowFieldColor(show bool) {
Std.SetShowFieldColor(show)
}
func GetShowFieldColor() bool {
return Std.GetShowFieldColor()
}
func SetFieldKeyColor(color []Attr) {
Std.SetFieldKeyColor(color)
}
func GetFieldKeyColor() []Attr {
return Std.GetFieldKeyColor()
}
func SetFieldTypeColor(fieldType string, color []Attr) {
Std.SetFieldTypeColor(fieldType, color)
}
func GetFieldTypeColors() map[string][]Attr {
return Std.GetFieldTypeColors()
}
func SetFieldValueColor(field string, color []Attr) {
Std.SetFieldValueColor(field, color)
}
func RemoveFieldValueColor(field string) {
Std.RemoveFieldValueColor(field)
}
func ClearFieldValueColors() {
Std.ClearFieldValueColors()
}
func GetFieldValueColors() map[string][]Attr {
return Std.GetFieldValueColors()
}
func WithField(key string, value interface{}) *StarLogger {
return Std.WithField(key, value)
}
func WithFields(fields Fields) *StarLogger {
return Std.WithFields(fields)
}
func WithError(err error) *StarLogger {
return Std.WithError(err)
}
func WithContext(ctx context.Context) *StarLogger {
return Std.WithContext(ctx)
}
func GetConfig() Config {
return Std.GetConfig()
}
func ApplyConfig(cfg Config) {
Std.ApplyConfig(cfg)
}
func UpdateConfig(update func(*Config)) {
Std.UpdateConfig(update)
}
func ApplyProductionConfig() {
Std.ApplyProductionConfig()
}
func ApplyDevelopmentConfig() {
Std.ApplyDevelopmentConfig()
}
func Flush() error {
return Std.Flush()
}
func Sync() error {
return Std.Sync()
}
// Shutdown gracefully drains async handlers and closes Std resources.
func Shutdown(ctx context.Context) error {
return Std.Shutdown(ctx)
}
// Deprecated: use Shutdown(ctx) for graceful exit.
func CloseStd() error {
return Std.Close()
}
func ShutdownStd(ctx context.Context) error {
return Shutdown(ctx)
}
func SetRateLimitConfig(cfg RateLimitConfig) {
Std.SetRateLimitConfig(cfg)
}
func GetRateLimitConfig() RateLimitConfig {
return Std.GetRateLimitConfig()
}
func EnableRateLimit(enable bool) {
Std.EnableRateLimit(enable)
}
func SetRateLimitDropHandler(handler func(RateLimitDropData)) {
Std.SetRateLimitDropHandler(handler)
}
func GetRateLimitStats() RateLimitStats {
return Std.GetRateLimitStats()
}
func ResetRateLimitStats() {
Std.ResetRateLimitStats()
}
func SetSamplingConfig(cfg SamplingConfig) {
Std.SetSamplingConfig(cfg)
}
func GetSamplingConfig() SamplingConfig {
return Std.GetSamplingConfig()
}
func EnableSampling(enable bool) {
Std.EnableSampling(enable)
}
func SetSamplingDropHandler(handler func(SamplingDropData)) {
Std.SetSamplingDropHandler(handler)
}
func GetSamplingStats() SamplingStats {
return Std.GetSamplingStats()
}
func ResetSamplingStats() {
Std.ResetSamplingStats()
}
func SetDedupConfig(cfg DedupConfig) {
Std.SetDedupConfig(cfg)
}
func GetDedupConfig() DedupConfig {
return Std.GetDedupConfig()
}
func EnableDedup(enable bool) {
Std.EnableDedup(enable)
}
func SetDedupDropHandler(handler func(DedupDropData)) {
Std.SetDedupDropHandler(handler)
}
func GetDedupStats() DedupStats {
return Std.GetDedupStats()
}
func ResetDedupStats() {
Std.ResetDedupStats()
}
func GetMetricsSnapshot() MetricsSnapshot {
return Std.GetMetricsSnapshot()
}
func NewStdTestHook() *TestHook {
return NewTestHook(Std)
}
func SetContextFieldExtractor(extractor func(context.Context) Fields) {
Std.SetContextFieldExtractor(extractor)
}
func DebugContext(ctx context.Context, str ...interface{}) {
stdmu.Lock()
defer stdmu.Unlock()
Std.isStd = true
Std.DebugContext(ctx, str...)
Std.isStd = false
}
func InfoContext(ctx context.Context, str ...interface{}) {
stdmu.Lock()
defer stdmu.Unlock()
Std.isStd = true
Std.InfoContext(ctx, str...)
Std.isStd = false
}
func NoticeContext(ctx context.Context, str ...interface{}) {
stdmu.Lock()
defer stdmu.Unlock()
Std.isStd = true
Std.NoticeContext(ctx, str...)
Std.isStd = false
}
func WarningContext(ctx context.Context, str ...interface{}) {
stdmu.Lock()
defer stdmu.Unlock()
Std.isStd = true
Std.WarningContext(ctx, str...)
Std.isStd = false
}
func ErrorContext(ctx context.Context, str ...interface{}) {
stdmu.Lock()
defer stdmu.Unlock()
Std.isStd = true
Std.ErrorContext(ctx, str...)
Std.isStd = false
}
func CriticalContext(ctx context.Context, str ...interface{}) {
stdmu.Lock()
defer stdmu.Unlock()
Std.isStd = true
Std.CriticalContext(ctx, str...)
Std.isStd = false
}
func LogContext(ctx context.Context, isShow bool, level int, str ...interface{}) {
stdmu.Lock()
defer stdmu.Unlock()
Std.isStd = true
Std.LogContext(ctx, isShow, level, str...)
Std.isStd = false
}
func SetRedactor(redactor Redactor) {
Std.SetRedactor(redactor)
}
func GetRedactor() Redactor {
return Std.GetRedactor()
}
func AddRedactRule(rule RedactRule) {
Std.AddRedactRule(rule)
}
func SetRedactRules(rules []RedactRule) {
Std.SetRedactRules(rules)
}
func ClearRedactRules() {
Std.ClearRedactRules()
}
func GetRedactRuleCount() int {
return Std.GetRedactRuleCount()
}
func SetRedactFailMode(mode RedactFailMode) {
Std.SetRedactFailMode(mode)
}
func GetRedactFailMode() RedactFailMode {
return Std.GetRedactFailMode()
}
func SetRedactMaskToken(mask string) {
Std.SetRedactMaskToken(mask)
}
func GetRedactMaskToken() string {
return Std.GetRedactMaskToken()
}
func GetRedactErrorCount() uint64 {
return Std.GetRedactErrorCount()
}
2020-12-21 17:22:45 +08:00
func Debug(str ...interface{}) {
stdmu.Lock()
defer stdmu.Unlock()
Std.isStd = true
Std.Debug(str...)
Std.isStd = false
}
func Debugf(format string, str ...interface{}) {
stdmu.Lock()
defer stdmu.Unlock()
Std.isStd = true
Std.Debugf(format, str...)
Std.isStd = false
}
func Debugln(str ...interface{}) {
stdmu.Lock()
defer stdmu.Unlock()
Std.isStd = true
Std.Debugln(str...)
Std.isStd = false
}
func Info(str ...interface{}) {
stdmu.Lock()
defer stdmu.Unlock()
Std.isStd = true
Std.Info(str...)
Std.isStd = false
}
func Infof(format string, str ...interface{}) {
stdmu.Lock()
defer stdmu.Unlock()
Std.isStd = true
Std.Infof(format, str...)
Std.isStd = false
}
func Infoln(str ...interface{}) {
stdmu.Lock()
defer stdmu.Unlock()
Std.isStd = true
Std.Infoln(str...)
Std.isStd = false
}
func Notice(str ...interface{}) {
stdmu.Lock()
defer stdmu.Unlock()
Std.isStd = true
Std.Notice(str...)
Std.isStd = false
}
func Noticef(format string, str ...interface{}) {
stdmu.Lock()
defer stdmu.Unlock()
Std.isStd = true
Std.Noticef(format, str...)
Std.isStd = false
}
func Noticeln(str ...interface{}) {
stdmu.Lock()
defer stdmu.Unlock()
Std.isStd = true
Std.Noticeln(str...)
Std.isStd = false
}
func Warning(str ...interface{}) {
stdmu.Lock()
defer stdmu.Unlock()
Std.isStd = true
Std.Warning(str...)
Std.isStd = false
}
func Warningf(format string, str ...interface{}) {
stdmu.Lock()
defer stdmu.Unlock()
Std.isStd = true
Std.Warningf(format, str...)
Std.isStd = false
}
func Warningln(str ...interface{}) {
stdmu.Lock()
defer stdmu.Unlock()
Std.isStd = true
Std.Warningln(str...)
Std.isStd = false
}
func Error(str ...interface{}) {
stdmu.Lock()
defer stdmu.Unlock()
Std.isStd = true
Std.Error(str...)
Std.isStd = false
}
func Errorf(format string, str ...interface{}) {
stdmu.Lock()
defer stdmu.Unlock()
Std.isStd = true
Std.Errorf(format, str...)
Std.isStd = false
}
func Errorln(str ...interface{}) {
stdmu.Lock()
defer stdmu.Unlock()
Std.isStd = true
Std.Errorln(str...)
Std.isStd = false
}
func Critical(str ...interface{}) {
stdmu.Lock()
defer stdmu.Unlock()
Std.isStd = true
Std.Critical(str...)
Std.isStd = false
}
func Criticalf(format string, str ...interface{}) {
stdmu.Lock()
defer stdmu.Unlock()
Std.isStd = true
Std.Criticalf(format, str...)
Std.isStd = false
}
func Criticalln(str ...interface{}) {
stdmu.Lock()
defer stdmu.Unlock()
Std.isStd = true
Std.Criticalln(str...)
Std.isStd = false
}
func Fatal(str ...interface{}) {
stdmu.Lock()
defer stdmu.Unlock()
Std.isStd = true
Std.Fatal(str...)
Std.isStd = false
}
func Fatalf(format string, str ...interface{}) {
stdmu.Lock()
defer stdmu.Unlock()
Std.isStd = true
Std.Fatalf(format, str...)
Std.isStd = false
}
func Panicln(str ...interface{}) {
stdmu.Lock()
defer stdmu.Unlock()
Std.isStd = true
2026-03-19 16:37:57 +08:00
Std.Panicln(str...)
2020-12-21 17:22:45 +08:00
Std.isStd = false
}
2020-12-22 11:07:20 +08:00
func Print(str ...interface{}) {
Std.Print(str...)
}
func Printf(format string, str ...interface{}) {
Std.Printf(format, str...)
}
func Println(str ...interface{}) {
Std.Println(str...)
}
2021-09-23 10:28:05 +08:00
func Log(isShow bool, level int, str ...interface{}) {
stdmu.Lock()
defer stdmu.Unlock()
Std.isStd = true
Std.Log(isShow, level, str...)
Std.isStd = false
}
func Logf(isShow bool, level int, format string, str ...interface{}) {
stdmu.Lock()
defer stdmu.Unlock()
Std.isStd = true
Std.Logf(isShow, level, format, str...)
Std.isStd = false
}
func Logln(isShow bool, level int, str ...interface{}) {
stdmu.Lock()
defer stdmu.Unlock()
Std.isStd = true
Std.Logln(isShow, level, str...)
Std.isStd = false
}
2020-12-22 11:07:20 +08:00
func StdPrint(attr []Attr, str ...interface{}) {
strs := fmt.Sprint(str...)
NewColor(attr...).Fprint(stdScreen, strs)
}
func StdPrintf(attr []Attr, format string, str ...interface{}) {
strs := fmt.Sprintf(format, str...)
NewColor(attr...).Fprint(stdScreen, strs)
}
func StdPrintln(attr []Attr, str ...interface{}) {
strs := fmt.Sprintln(str...)
NewColor(attr...).Fprint(stdScreen, strs)
}
2020-12-21 17:22:45 +08:00
func SetWriter(wr io.Writer) {
Std.SetWriter(wr)
}
func GetWriter() io.Writer {
return Std.GetWriter()
}
2026-03-19 16:37:57 +08:00
func AsWriter(level int) io.Writer {
return Std.AsWriter(level)
}
func AsWriterWithOptions(level int, opts ...StdlibBridgeOption) io.Writer {
return Std.AsWriterWithOptions(level, opts...)
}
func AsStdlibLogger(level int) *log.Logger {
return Std.AsStdlibLogger(level)
}
func AsStdlibLoggerWithOptions(level int, opts ...StdlibBridgeOption) *log.Logger {
return Std.AsStdlibLoggerWithOptions(level, opts...)
}
func SetSink(sink Sink) {
Std.SetSink(sink)
}
func SetSinks(sinks ...Sink) {
Std.SetSinks(sinks...)
}
func GetSink() Sink {
return Std.GetSink()
}
func SetFormatter(formatter Formatter) {
Std.SetFormatter(formatter)
}
func GetFormatter() Formatter {
return Std.GetFormatter()
}
func SetEntryHandler(handler Handler) {
Std.SetEntryHandler(handler)
}
func GetEntryHandler() Handler {
return Std.GetEntryHandler()
}
func AppendEntryHandler(handler Handler) {
Std.AppendEntryHandler(handler)
}
func SetHandler(f func(LogData)) {
2020-12-21 17:22:45 +08:00
Std.SetHandler(f)
}
func GetHandler() func(LogData) {
2020-12-21 17:22:45 +08:00
return Std.GetHandler()
}
2026-03-19 16:37:57 +08:00
func SetPendingWriteLimit(limit int) {
Std.SetPendingWriteLimit(limit)
}
func GetPendingWriteLimit() int {
return Std.GetPendingWriteLimit()
}
func SetPendingDropPolicy(policy PendingDropPolicy) {
Std.SetPendingDropPolicy(policy)
}
func GetPendingDropPolicy() PendingDropPolicy {
return Std.GetPendingDropPolicy()
}
func GetPendingDropCount() uint64 {
return Std.GetPendingDropCount()
}
func GetPendingBlockCount() uint64 {
return Std.GetPendingBlockCount()
}
func GetPendingPeakLength() int {
return Std.GetPendingPeakLength()
}
func GetPendingStats() PendingStats {
return Std.GetPendingStats()
}
func SetEntryHandlerTimeout(timeout time.Duration) {
Std.SetEntryHandlerTimeout(timeout)
}
func GetEntryHandlerTimeout() time.Duration {
return Std.GetEntryHandlerTimeout()
}
2020-12-21 17:22:45 +08:00
func SetSwitching(sw bool) {
Std.SetSwitching(sw)
}
func SetShowOriginFile(val bool) {
Std.SetShowOriginFile(val)
}
func GetShowOriginFile() bool {
return Std.GetShowOriginFile()
}
func SetShowFuncName(val bool) {
2026-03-19 16:37:57 +08:00
Std.SetShowFuncName(val)
2020-12-21 17:22:45 +08:00
}
func GetShowFuncName() bool {
2026-03-19 16:37:57 +08:00
return Std.GetShowFuncName()
2020-12-21 17:22:45 +08:00
}
func SetShowLevel(val bool) {
Std.SetShowLevel(val)
}
func GetShowLevel() bool {
return Std.GetShowLevel()
}
func SetShowFlag(val bool) {
Std.SetShowFlag(val)
}
func GetShowFlag() bool {
return Std.GetShowFlag()
}
func SetShowStd(val bool) {
Std.SetShowStd(val)
}
func GetShowStd() bool {
return Std.GetShowStd()
}
2026-03-19 16:37:57 +08:00
func SetAutoAppendNewline(enable bool) {
Std.SetAutoAppendNewline(enable)
}
func GetAutoAppendNewline() bool {
return Std.GetAutoAppendNewline()
}
2020-12-21 17:22:45 +08:00
func StopWrite() {
Std.StopWrite()
}
2026-03-19 16:37:57 +08:00
func EnableWrite() {
Std.EnableWrite()
}
func IsWriteStopped() bool {
return Std.IsWriteStopped()
}
// Deprecated: use EnableWrite.
2020-12-21 17:22:45 +08:00
func EnbaleWrite() {
Std.EnbaleWrite()
}
2026-03-19 16:37:57 +08:00
// Deprecated: use IsWriteStopped.
2020-12-21 17:22:45 +08:00
func IsWriteStoed() bool {
return Std.IsWriteStoed()
}