本文整理匯總了Golang中github.com/purstal/go-tieba-base/logs.Logger類的典型用法代碼示例。如果您正苦於以下問題:Golang Logger類的具體用法?Golang Logger怎麽用?Golang Logger使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Logger類的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: LoadStrings
func LoadStrings(file *os.File, set *map[string]struct{}, logger *logs.Logger) error {
bytes, err := ReadAll(file)
if err != nil {
return err
}
lines := split([]rune(string(bytes)), []rune{'\n', ' '})
newSet := map[string]struct{}{}
oldSet := map[string]struct{}{}
for k, v := range *set {
oldSet[k] = v //複製一遍
}
var added []string
for _, _line := range lines {
var line string
if line = strings.TrimSpace(string(_line)); line == "" {
continue
}
newSet[line] = struct{}{}
if _, exist := oldSet[line]; !exist {
added = append(added, line)
}
delete(oldSet, line)
}
*set = newSet
var updateInfo string = fmt.Sprintf("更新關鍵詞(%s):", file.Name())
if len(added) > 0 {
updateInfo = updateInfo + "\n[+]"
for _, str := range added {
updateInfo = updateInfo + str + " "
}
updateInfo = strings.TrimRight(updateInfo, " ")
}
if len(oldSet) > 0 {
updateInfo = updateInfo + "\n[-]"
for str, _ := range oldSet {
updateInfo = updateInfo + str + " "
}
updateInfo = strings.TrimRight(updateInfo, " ")
}
logger.Info(updateInfo)
return nil
}
示例2: newStrKwManager
func newStrKwManager(fileName string, logger *logs.Logger) *kw_manager.StringKeywordManager {
var m *kw_manager.StringKeywordManager
var err error
if fileName != "" {
m, err =
kw_manager.NewStringKeywordManagerBidingWithFile(
fileName, time.Second, logger)
if err != nil {
logger.Error("無法創建string關鍵詞管理.", err)
panic(err)
}
return m
} else {
logger.Warn("未設置string關鍵詞文件")
return kw_manager.NewStringKeywordManager(logger)
}
}
示例3: NewMessageMonitor
func NewMessageMonitor(logger *logs.Logger, interval time.Duration, acc *postbar.Account, lastFoundPid uint64) *MessageMonitor {
var monitor MessageMonitor
monitor.Interval = interval
monitor.MessageChan = make(chan message.ReplyMessage)
monitor.actChan = make(chan action.Action)
go func() {
ticker := time.NewTicker(monitor.Interval)
for {
msgs, _lastFoundPid, err, pberr := checkReply(acc, lastFoundPid)
if len(msgs) > 0 {
if err != nil {
logger.Error("無法獲取消息提醒", err)
continue
} else if pberr != nil && pberr.ErrorCode != 0 {
logger.Error("無法獲取消息提醒", pberr)
continue
}
lastFoundPid = _lastFoundPid
for _, msg := range msgs {
monitor.MessageChan <- msg
}
}
select {
case <-ticker.C:
case act := <-monitor.actChan:
switch act.Pattern {
case Stop:
ticker.Stop()
//logs.Debug("喵")
close(monitor.MessageChan)
//logs.Debug("喵喵")
close(monitor.actChan)
//logs.Debug("將解引賦值:", lastFoundPid)
*(act.Param.(*chan uint64)) <- lastFoundPid
return
}
}
}
}()
return &monitor
}
示例4: NewRegexpKeywordManagerBidingWithFile
func NewRegexpKeywordManagerBidingWithFile(keyWordFileFlieName string,
checkInterval time.Duration, logger *logs.Logger) (*RegexpKeywordManager, error) {
var m RegexpKeywordManager
m.FileName = keyWordFileFlieName
file, err := os.Open(m.FileName)
if os.IsNotExist(err) {
var err error
file, err = os.Create(m.FileName)
if err != nil {
return nil, err
}
} else if err != nil {
return nil, err
} else {
err := LoadExps(file, &m.KewWordExps, logger)
if err != nil {
return nil, err
}
}
if fi, err := file.Stat(); err != nil {
return nil, err
} else {
m.LastModTime = fi.ModTime()
}
file.Close()
m.CheckInterval = checkInterval
m.actChan = make(chan action.Action)
go func() {
ticker := time.NewTicker(m.CheckInterval)
for {
select {
case <-ticker.C:
case act := <-m.actChan:
switch act.Pattern {
case ChangeInterval:
ticker.Stop()
ticker = time.NewTicker(act.Param.(time.Duration))
case ChangeFile:
}
continue
}
file, err1 := os.Open(m.FileName)
if err1 != nil {
logger.Error("無法打開關鍵詞文件,跳過本次.", err1, ".")
continue
}
func() {
defer func() { file.Close() }()
fi, err2 := file.Stat()
if err2 != nil {
logger.Error("無法獲取文件信息,跳過本次.", err2, ".")
return
}
if modTime := fi.ModTime(); modTime != m.LastModTime {
err := LoadExps(file, &m.KewWordExps, logger)
if err != nil {
logger.Error("無法更新關鍵詞,下次修改前將不嘗試讀取.", err, ".")
}
m.LastModTime = modTime
}
}()
}
}()
return &m, nil
}
示例5: LoadExps
func LoadExps(file *os.File, exps *[]RegexpKeyword, logger *logs.Logger) error {
bytes, err := ReadAll(file)
if err != nil {
return err
}
lines := strings.Split(string(bytes), "\n")
oldExps := make(map[string]RegexpKeyword)
for _, exp := range *exps {
if exp.BanFlag {
oldExps["$ban "+exp.Rx.String()] = exp
} else {
oldExps[exp.Rx.String()] = exp
}
}
newExps := make(map[string]RegexpKeyword)
var addedExps []string
for lineNo, line := range lines {
line = strings.TrimRightFunc(line, func(r rune) bool {
return r == '\n' || r == '\r'
})
if line == "" {
continue
}
if exp, exist := oldExps[line]; exist {
newExps[line] = exp
delete(oldExps, line)
} else {
var banFlag bool
var newExp *regexp.Regexp
var err error
if banFlag = strings.HasPrefix(line, "$ban "); banFlag {
newExp, err = regexp.Compile(strings.TrimLeft(line, "$ban "))
} else {
newExp, err = regexp.Compile(line)
}
if err != nil {
logs.Error(fmt.Sprintf("不正確的關鍵詞(第%d行),跳過.", lineNo), err)
} else {
newExps[line] = RegexpKeyword{banFlag, newExp}
addedExps = append(addedExps, line)
}
}
}
newExpSlice := make([]RegexpKeyword, 0, len(newExps))
for _, exp := range newExps {
newExpSlice = append(newExpSlice, exp)
}
*exps = newExpSlice
var updateInfo string = fmt.Sprintf("更新關鍵詞(%s):\n", file.Name())
for _, exp := range addedExps {
updateInfo = updateInfo + "[+] " + exp + "\n"
}
for _, exp := range oldExps {
if exp.BanFlag {
updateInfo = updateInfo + "[-] $ban" + exp.Rx.String() + "\n"
} else {
updateInfo = updateInfo + "[-] " + exp.Rx.String() + "\n"
}
}
updateInfo = strings.TrimSuffix(updateInfo, "\n")
logger.Info(updateInfo)
//logger.Debug("現在的關鍵詞:", newExpSlice, ".")
return nil
}