utils

package
v0.0.0-...-9b258a7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 20, 2025 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

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.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CleanJSON

func CleanJSON(jsonStr string) string

CleanJSON 清理非标准JSON字符串,使其符合标准格式 参数:

  • jsonStr: 可能包含非标准格式的JSON字符串

返回:

  • string: 清理后的标准JSON字符串

func ClearGlobalVars

func ClearGlobalVars()

ClearGlobalVars 清除所有全局变量

func Debug

func Debug(msg string, fields ...zap.Field)

Debug 输出调试级别日志

func Error

func Error(msg string, fields ...zap.Field)

Error 输出错误级别日志

func ExtractField

func ExtractField(jsonStr, fieldName string) (string, error)

ExtractField 从JSON字符串中提取特定字段 参数:

  • jsonStr: JSON字符串
  • fieldName: 要提取的字段名

返回:

  • string: 提取的字段值
  • error: 提取错误

func ExtractYaml

func ExtractYaml(message string) string

ExtractYaml extracts yaml from a markdown message.

func Fatal

func Fatal(msg string, fields ...zap.Field)

Fatal 输出致命错误日志并退出程序

func GetConfig

func GetConfig() *viper.Viper

GetConfig 获取配置实例

func GetGlobalVar

func GetGlobalVar(key string) (interface{}, bool)

GetGlobalVar 获取全局变量

func GetLogger

func GetLogger() *zap.Logger

GetLogger 获取全局日志记录器

func Info

func Info(msg string, fields ...zap.Field)

Info 输出信息级别日志

func InitConfig

func InitConfig() error

InitConfig 初始化配置

func InitLogger

func InitLogger(config *LogConfig) (*zap.Logger, error)

InitLogger 初始化日志系统

func ParseJSON

func ParseJSON(jsonStr string) (map[string]interface{}, error)

ParseJSON 解析JSON字符串为map[string]interface{} 参数:

  • jsonStr: JSON字符串

返回:

  • map[string]interface{}: 解析后的对象
  • error: 解析错误

func RemoveGlobalVar

func RemoveGlobalVar(key string)

RemoveGlobalVar 删除全局变量

func RenderMarkdown

func RenderMarkdown(md string) error

RenderMarkdown renders markdown to the terminal.

func SetGlobalVar

func SetGlobalVar(key string, value interface{})

SetGlobalVar 设置全局变量

func Sync

func Sync() error

Sync 同步日志缓冲区到输出

func Warn

func Warn(msg string, fields ...zap.Field)

Warn 输出警告级别日志

func With

func With(fields ...zap.Field) *zap.Logger

With 创建带有额外字段的日志记录器

Types

type LogConfig

type LogConfig struct {
	// 日志级别
	Level zapcore.Level
	// 日志目录
	LogDir string
	// 日志文件名
	Filename string
	// 单个日志文件最大大小,单位MB
	MaxSize int
	// 保留的旧日志文件最大数量
	MaxBackups int
	// 保留的日志文件最大天数
	MaxAge int
	// 是否压缩旧日志文件
	Compress bool
	// 是否在控制台输出
	ConsoleOutput bool
	// 是否使用彩色日志
	ColoredOutput bool
}

LogConfig 日志配置

func DefaultLogConfig

func DefaultLogConfig() *LogConfig

DefaultLogConfig 返回默认日志配置

type PerfStats

type PerfStats struct {
	// contains filtered or unexported fields
}

PerfStats 性能统计结构体 用于收集和分析系统各个部分的性能数据

func GetPerfStats

func GetPerfStats() *PerfStats

GetPerfStats 获取全局性能统计实例 返回:

  • *PerfStats: 全局性能统计实例

func (*PerfStats) GetMetricStats

func (p *PerfStats) GetMetricStats(operation string) (min, max, avg, p95, p99 time.Duration, count int, total time.Duration)

GetMetricStats 获取特定操作的统计信息 参数:

  • operation: 操作名称

返回:

  • min: 最小耗时
  • max: 最大耗时
  • avg: 平均耗时
  • p95: 95百分位耗时
  • p99: 99百分位耗时
  • count: 操作次数
  • total: 总耗时

func (*PerfStats) GetMetrics

func (p *PerfStats) GetMetrics() map[string][]time.Duration

GetMetrics 获取所有性能指标 返回:

  • map[string][]time.Duration: 所有操作的耗时记录

func (*PerfStats) GetStats

func (p *PerfStats) GetStats() map[string]interface{}

GetStats 获取所有性能统计信息

func (*PerfStats) PrintStats

func (p *PerfStats) PrintStats() string

PrintStats 打印所有性能统计信息 返回:

  • string: 格式化的统计信息

func (*PerfStats) RecordMetric

func (p *PerfStats) RecordMetric(operation string, duration time.Duration)

RecordMetric 直接记录一个性能指标 参数:

  • operation: 操作名称
  • duration: 操作耗时

func (*PerfStats) Reset

func (p *PerfStats) Reset()

Reset 重置所有性能统计信息

func (*PerfStats) ResetMetrics

func (p *PerfStats) ResetMetrics()

ResetMetrics 重置所有性能指标

func (*PerfStats) SetEnableLogging

func (p *PerfStats) SetEnableLogging(enable bool)

SetEnableLogging 设置是否启用日志记录 参数:

  • enable: 是否启用

func (*PerfStats) SetLogger

func (p *PerfStats) SetLogger(logger *zap.Logger)

SetLogger 设置日志记录器 参数:

  • logger: zap日志记录器

func (*PerfStats) StartTimer

func (p *PerfStats) StartTimer(operation string)

StartTimer 开始计时特定操作 参数:

  • operation: 操作名称

func (*PerfStats) StopTimer

func (p *PerfStats) StopTimer(operation string) time.Duration

StopTimer 停止计时特定操作并记录耗时 参数:

  • operation: 操作名称

返回:

  • time.Duration: 操作耗时

func (*PerfStats) TraceFunc

func (p *PerfStats) TraceFunc(operation string) func()

TraceFunc 是一个辅助函数,用于跟踪函数执行时间 使用方法:defer utils.GetPerfStats().TraceFunc("函数名称")() 参数:

  • operation: 操作名称

返回:

  • func(): 在函数结束时调用的函数

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL