utility

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2022 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	LogTypeFatal   = 1
	LogTypeWarning = 2
	LogTypeNotice  = 4
	LogTypeTrace   = 8
	LogTypeDebug   = 16
)

日志级别类型常量

View Source
const (
	LogTypeFatalStr   = "FATAL"
	LogTypeWarningStr = "WARNING"
	LogTypeNoticeStr  = "NOTICE"
	LogTypeTraceStr   = "TRACE"
	LogTypeDebugStr   = "DEBUG"
)

日志类型对应信息

View Source
const (
	LogConfNoticeFilePath  = "log_notice_file_path"
	LogConfDebugFilePath   = "log_debug_file_path"
	LogConfTraceFilePath   = "log_trace_file_path"
	LogConfFatalFilePath   = "log_fatal_file_path"
	LogConfWarningFilePath = "log_warning_file_path"

	LogConfCronTime     = "log_cron_time"
	LogConfChanBuffSize = "log_channel_buff_size"
	LogConfFlushTimer   = "log_flush_timer"
	LogConfDebugOpen    = "log_debug_open"
	LogConfLevel        = "log_level"
	LogConfFileLifeTime = "log_file_life_time"
)

*

  • 配置项相关常量&变量
View Source
const (
	LogConfTypeStr = 1
	LogConfTypeNum = 2
)

配置选项值类型(字符串或数字)

Variables

GConfFileToTypeMap 日志文件名与日志类型的映射

GConfItemMap 配置项map全局变量 (定义一个选项输入的值是字符串还是数字)

View Source
var GFlushLock *sync.Mutex = &sync.Mutex{}

GFlushLock 控制 GFlushLogFlag 的全局锁

View Source
var GFlushLogFlag bool = false

GFlushLogFlag 目前是否已经写入刷盘操作channel(保证全局只能写入一次,防止多协程操作阻塞)

GLogTypeMap 日志信息map

View Source
var GOnceV sync.Once

GOnceV 全局once

Functions

func LogDebugPrint

func LogDebugPrint(msg string, v interface{})

LogDebugPrint . 日志打印输出到终端函数

func LogGetFileSuffix

func LogGetFileSuffix(now time.Time) string

LogGetFileSuffix . 获取日志文件的切割时间 说明:

目前主要支持三种粒度的设置,基本这些粒度足够我们使用了

1天:day; 1小时:hour; 10分钟:ten

func LogGetOsSeparator

func LogGetOsSeparator(pathName string) string

LogGetOsSeparator . 获取当前操作系统的路径切割符 说明: 主要为了解决 os.PathSeparator有些时候无法满足要求的问题

func LogInit

func LogInit()

LogInit . 初始化Log协程相关操作 注意: 全局操作, 只能协程初始化的时候调用一次

func LogIsDebug

func LogIsDebug() bool

LogIsDebug . 获取目前是否是Debug模式

func LogOpenFile

func LogOpenFile() error

LogOpenFile .

打开&切割日志文件

func LogRun

func LogRun(RunConfigMap map[string]string)

LogRun . 注意: 需要传递进来的配置是有要求的,必须是包含这些配置选项,否则会报错

func LogWriteFile

func LogWriteFile(logMsg LogMsgT)

LogWriteFile . 写日志操作

func LogfileCleanup

func LogfileCleanup(fileLifetime int64)

LogfileCleanup . 对notice日志,进行定期清理,执行周期等同于“日志切割周期”

func RegisterRpcTypeForValue

func RegisterRpcTypeForValue(value interface{})

RegisterRpcTypeForValue 注册 rpc 类型

Types

type Config

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

Config .

func NewConfig

func NewConfig() *Config

NewConfig .

func (*Config) Get

func (c *Config) Get(key string) string

Get .

func (*Config) GetAll

func (c *Config) GetAll() map[string]string

GetAll .

func (*Config) GetInt

func (c *Config) GetInt(key string) int

GetInt .

func (*Config) GetInt64

func (c *Config) GetInt64(key string) int64

GetInt64 .

func (*Config) GetSlice

func (c *Config) GetSlice(key string, separator string) []string

GetSlice .

func (*Config) GetSliceInt

func (c *Config) GetSliceInt(key string, separator string) []int

GetSliceInt .

func (*Config) Load

func (c *Config) Load(configFile string) error

Load .

type HttpClient

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

HttpClient .

func NewHttpClient

func NewHttpClient() *HttpClient

NewHttpClient .

func (*HttpClient) Close

func (c *HttpClient) Close() error

Close .

func (*HttpClient) ConnectTo

func (c *HttpClient) ConnectTo(address string, timeout time.Duration) error

ConnectTo .

func (*HttpClient) Dial

func (c *HttpClient) Dial(localAddress string, remoteAddress string, timeout time.Duration) error

Dial .

func (*HttpClient) Get

func (c *HttpClient) Get(uri string, readTimeout time.Duration, writeTimeout time.Duration) (*HttpResponse, error)

Get .

func (*HttpClient) Post

func (c *HttpClient) Post(uri string, posts map[string]string, readTimeout time.Duration, writeTimeout time.Duration) (*HttpResponse, error)

Post .

func (*HttpClient) Recv

func (c *HttpClient) Recv(readTimeout time.Duration) (*HttpResponse, error)

Recv .

func (*HttpClient) Send

func (c *HttpClient) Send(request *HttpRequest, writeTimeout time.Duration) error

Send .

func (*HttpClient) SetDebug

func (c *HttpClient) SetDebug(debug bool)

SetDebug .

type HttpConnection

type HttpConnection struct {
	*TcpConnection // 内嵌 *TcpConnection
	// contains filtered or unexported fields
}

HttpConnection .

func NewHttpConnection

func NewHttpConnection(tcpConnection *TcpConnection) *HttpConnection

NewHttpConnection .

func (*HttpConnection) ReadRequest

func (c *HttpConnection) ReadRequest(timeout time.Duration) (*HttpRequest, error)

ReadRequest .

func (*HttpConnection) ReadResponse

func (c *HttpConnection) ReadResponse(timeout time.Duration) (*HttpResponse, error)

ReadResponse .

func (*HttpConnection) WriteRequest

func (c *HttpConnection) WriteRequest(request *HttpRequest, timeout time.Duration) error

WriteRequest .

func (*HttpConnection) WriteResponse

func (c *HttpConnection) WriteResponse(response *HttpResponse, timeout time.Duration) error

WriteResponse .

type HttpMessage

type HttpMessage interface {
	SetHeader(key string, value string)
	Header(key string) string
	Stream() []byte
	// contains filtered or unexported methods
}

HttpMessage .

type HttpRequest

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

HttpRequest .

func NewHttpRequest

func NewHttpRequest() *HttpRequest

NewHttpRequest .

func (*HttpRequest) Chas

func (r *HttpRequest) Chas(key string) bool

Chas .

func (*HttpRequest) Cint

func (r *HttpRequest) Cint(key string) int

Cint .

func (*HttpRequest) Cookies

func (r *HttpRequest) Cookies() map[string]string

Cookies .

func (*HttpRequest) Cstr

func (r *HttpRequest) Cstr(key string) string

Cstr .

func (*HttpRequest) GetRemoteIP

func (r *HttpRequest) GetRemoteIP() string

GetRemoteIP .

func (*HttpRequest) Gets

func (r *HttpRequest) Gets() map[string]string

Gets .

func (*HttpRequest) Ghas

func (r *HttpRequest) Ghas(key string) bool

Ghas .

func (*HttpRequest) Gint

func (r *HttpRequest) Gint(key string) int

Gint .

func (*HttpRequest) Gstr

func (r *HttpRequest) Gstr(key string) string

Gstr .

func (*HttpRequest) Header

func (r *HttpRequest) Header(key string) string

Header .

func (*HttpRequest) IsDelete

func (r *HttpRequest) IsDelete() bool

IsDelete .

func (*HttpRequest) IsGet

func (r *HttpRequest) IsGet() bool

IsGet .

func (*HttpRequest) IsKeepAlive

func (r *HttpRequest) IsKeepAlive() bool

IsKeepAlive .

func (*HttpRequest) IsPost

func (r *HttpRequest) IsPost() bool

IsPost .

func (*HttpRequest) IsPut

func (r *HttpRequest) IsPut() bool

IsPut .

func (*HttpRequest) Method

func (r *HttpRequest) Method() string

Method .

func (*HttpRequest) PathInfo

func (r *HttpRequest) PathInfo() string

PathInfo .

func (*HttpRequest) Phas

func (r *HttpRequest) Phas(key string) bool

Phas .

func (*HttpRequest) Pint

func (r *HttpRequest) Pint(key string) int

Pint .

func (*HttpRequest) Posts

func (r *HttpRequest) Posts() map[string]string

Posts .

func (*HttpRequest) Pstr

func (r *HttpRequest) Pstr(key string) string

Pstr .

func (*HttpRequest) Rint

func (r *HttpRequest) Rint(key string) int

Rint .

func (*HttpRequest) Rstr

func (r *HttpRequest) Rstr(key string) string

Rstr .

func (*HttpRequest) SetHeader

func (r *HttpRequest) SetHeader(key string, value string)

SetHeader .

func (*HttpRequest) SetMethod

func (r *HttpRequest) SetMethod(method string)

SetMethod .

func (*HttpRequest) SetPosts

func (r *HttpRequest) SetPosts(posts map[string]string)

SetPosts .

func (*HttpRequest) SetUri

func (r *HttpRequest) SetUri(uri string)

SetUri .

func (*HttpRequest) SetVar

func (r *HttpRequest) SetVar(key string, value interface{})

SetVar 。

func (*HttpRequest) SetVersion

func (r *HttpRequest) SetVersion(version string)

SetVersion .

func (*HttpRequest) Stream

func (r *HttpRequest) Stream() []byte

Stream 。

func (*HttpRequest) String

func (r *HttpRequest) String() string

String 。

func (*HttpRequest) Uri

func (r *HttpRequest) Uri() string

Uri .

func (*HttpRequest) Var

func (r *HttpRequest) Var(key string) interface{}

Var 。

func (*HttpRequest) Version

func (r *HttpRequest) Version() string

Version .

type HttpResponse

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

HttpResponse .

func NewHttpResponse

func NewHttpResponse() *HttpResponse

NewHttpResponse .

func (*HttpResponse) BodyStream

func (r *HttpResponse) BodyStream() []byte

BodyStream .

func (*HttpResponse) BodyString

func (r *HttpResponse) BodyString() string

BodyString .

func (*HttpResponse) Code

func (r *HttpResponse) Code() int

Code .

func (*HttpResponse) Header

func (r *HttpResponse) Header(key string) string

Header .

func (*HttpResponse) IsConnectionClose

func (r *HttpResponse) IsConnectionClose() bool

IsConnectionClose .

func (*HttpResponse) Putb

func (r *HttpResponse) Putb(stream []byte)

Putb .

func (*HttpResponse) Puts

func (r *HttpResponse) Puts(content string)

Puts .

func (*HttpResponse) SetBodyStream

func (r *HttpResponse) SetBodyStream(bodyStream []byte)

SetBodyStream .

func (*HttpResponse) SetBodyString

func (r *HttpResponse) SetBodyString(bodyString string)

SetBodyString .

func (*HttpResponse) SetCode

func (r *HttpResponse) SetCode(code int)

SetCode .

func (*HttpResponse) SetHeader

func (r *HttpResponse) SetHeader(key string, value string)

SetHeader .

func (*HttpResponse) SetStatus

func (r *HttpResponse) SetStatus(code int, phrase string)

SetStatus .

func (*HttpResponse) SetVersion

func (r *HttpResponse) SetVersion(version string)

SetVersion .

func (*HttpResponse) Status

func (r *HttpResponse) Status() (int, string)

Status .

func (*HttpResponse) Stream

func (r *HttpResponse) Stream() []byte

Stream 。

func (*HttpResponse) String

func (r *HttpResponse) String() string

String . return string

func (*HttpResponse) Version

func (r *HttpResponse) Version() string

Version .

type LogMsgT

type LogMsgT struct {
	LogType int
	LogData string
}

LogMsgT 单条日志结构

type LogT

type LogT struct {

	// 日志接收channel队列
	LogChan chan LogMsgT

	// 是否马上日志刷盘: true or false,如果为true,则马上日志刷盘 (本chan暂时没有使用)
	FlushLogChan chan bool

	// 所有日志文件位置
	LogFilePath map[int]string

	// 日志文件位置 (例:/var/log/koala.log 和 /var/log/koala.log.wf)
	LogNoticeFilePath string
	LogErrorFilePath  string

	// 写入日志切割周期(1天:day、1小时:hour、15分钟:Fifteen、10分钟:Ten)
	LogCronTime string

	// 日志chan队列的buffer长度,建议不要少于1024,不多于102400,最长:2147483648
	LogChanBuffSize int

	// 按照间隔时间日志刷盘的日志的间隔时间,单位:秒,建议1~5秒,不超过256
	LogFlushTimer int

	// 去重的日志文件名和fd (实际需需要物理写入文件名和句柄)
	MergeLogFile map[string]string
	MergeLogFd   map[string]*os.File

	// 上游配置的map数据(必须包含所有所需项)
	RunConfigMap map[string]string

	// 是否开启日志库调试模式
	LogDebugOpen bool

	// 日志打印的级别(需要打印那些日志)
	LogLevel int

	// 日志文件的存在时间, 单位:天
	LogLifeTime int
}

LogT Log主chan队列配置

var GLogV *LogT

GLogV .日志全局变量

type Logger

type Logger struct {
	LogID string
}

Logger .Log 每次请求结构体数据

func NewLogger

func NewLogger(logid string) *Logger

NewLogger . 构造函数

func (*Logger) Debug

func (l *Logger) Debug(logMessage string)

Debug . 函数调用调试debug日志打印调用

func (*Logger) Fatal

func (l *Logger) Fatal(logMessage string)

Fatal . 致命错误Fatal日志打印调用

func (*Logger) Notice

func (l *Logger) Notice(logMessage string)

Notice . 正常请求日志打印调用 注意: 每个请求(request)只能调用本函数一次,函数里必须携带必须字段: ip, errno, errmsg 等字段,其他kv信息自己组织 示例: Log_Notice("clientip=192.168.0.1 errno=0 errmsg=ok key1=valu2 key2=valu2")

func (*Logger) Trace

func (l *Logger) Trace(logMessage string)

Trace . 函数调用栈trace日志打印调用

func (*Logger) Warning

func (l *Logger) Warning(logMessage string)

Warning . 警告错误warging日志打印调用

type RpcRequest

type RpcRequest struct {
	Func string
	Args map[string]interface{}
}

RpcRequest .

func NewRpcRequest

func NewRpcRequest() *RpcRequest

NewRpcRequest .

type RpcResponse

type RpcResponse struct {
	Result bool
	Reason string
	Code   int
	Args   map[string]interface{}
	Data   interface{}
}

RpcResponse .

func NewRpcResponse

func NewRpcResponse() *RpcResponse

NewRpcResponse .

type TcpConnection

type TcpConnection struct {
	*net.TCPConn
	// contains filtered or unexported fields
}

TcpConnection tcp 连接

func TcpConnect

func TcpConnect(localAddress string, remoteAddress string, timeout time.Duration) (*TcpConnection, error)

TcpConnect 连接一个 tcp 地址

func (*TcpConnection) IsHttpProto

func (c *TcpConnection) IsHttpProto() bool

IsHttpProto .

func (*TcpConnection) Read

func (c *TcpConnection) Read(p []byte) (n int, err error)

Read .

func (*TcpConnection) ReadProtoBuffer

func (c *TcpConnection) ReadProtoBuffer() error

ReadProtoBuffer .

func (*TcpConnection) ReadRpcRequest

func (c *TcpConnection) ReadRpcRequest(timeout time.Duration) (*RpcRequest, error)

ReadRpcRequest .

func (*TcpConnection) ReadStream

func (c *TcpConnection) ReadStream(stream []byte, count int) error

ReadStream .

func (*TcpConnection) Rpc

func (c *TcpConnection) Rpc(request *RpcRequest, readTimeout time.Duration, writeTimeout time.Duration) (*RpcResponse, error)

Rpc .

func (*TcpConnection) WriteRpcResponse

func (c *TcpConnection) WriteRpcResponse(response *RpcResponse, timeout time.Duration) error

WriteRpcResponse .

type TcpListener

type TcpListener struct {
	*net.TCPListener
}

TcpListener tcp 监听器

func TcpListen

func TcpListen(address string) (*TcpListener, error)

TcpListen 监听一个 tcp 地址

func (*TcpListener) Accept

func (l *TcpListener) Accept() (*TcpConnection, error)

Accept .

Jump to

Keyboard shortcuts

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