func execLevelExamples() { // 默认日志级别 debug log.Printf("default log level: %s", log.GetLevel()) log.Tracef("IsTraceEnabled? %t", log.IsTraceEnabled()) log.Debugf("IsDebugEnabled? %t", log.IsDebugEnabled()) log.Infof("IsInfoEnabled? %t", log.IsInfoEnabled()) // trace 级别 log.SetLevel(log.Ltrace) log.Tracef(msgFmt, 1) // info 级别 log.SetLevel(log.Linfo) log.Debugf(msgFmt, 2) log.Infof(msgFmt, 2) // warn 级别 log.SetLevel(log.Lwarn) log.Infof(msgFmt, 3) log.Warnf(msgFmt, 3) // error 级别 log.SetLevel(log.Lerror) log.Warnf(msgFmt, 4) log.Errorf(msgFmt, 4) // 恢复默认级别,防止影响其他测试 // debug 级别 log.SetLevel(log.Ldebug) log.Tracef(msgFmt, 5) log.Debugf(msgFmt, 5) }
func main() { log.Debugf("this is a test message, %d", 1111) format := fmt.Sprintf("%s %s %s %s:%d %s", "2006-01-02 15:04:05.000000", log.TagToken, log.LevelToken, log.ProjectToken, log.LineToken, log.MessageToken) log.ChangeFormat(format) log.Tinfof("6ba7b814-9dad-11d1-80b4-00c04fd430c8", "this is a test message, %d", 1111) format = fmt.Sprintf(`{"date": "%s", "time": "%s", "level": "%s", "file": "%s", "line": %d, "log": "%s"}`, "2006-01-02", "15:04:05.999", log.LevelToken, log.ProjectToken, log.LineToken, log.MessageToken) log.ChangeFormat(format) log.Infof("this is a test message, %d", 1111) format = fmt.Sprintf(`<log><date>%s</date><time>%s</time><level>%s</level><file>%s</file><line>%d</line><msg>%s</msg><log>`, "2006-01-02", "15:04:05.000", log.LevelToken, log.ProjectToken, log.LineToken, log.MessageToken) log.ChangeFormat(format) log.Tinfof("6ba7b814-9dad-11d1-80b4-00c04fd430c8", "this is a test message, %d", 1111) log.Error("level = debug") log.Infof("this is a test message, %d", 1111) log.Errorf("this is another test message, %d", 22222) // Fatalf("%d %s", log.FatalLevel, log.FatalLevel) format = fmt.Sprintf("%s %s %s %s:%d %s", "2006-1-2", "3:4:05.9", log.LevelToken, log.PathToken, log.LineToken, log.MessageToken) log.ChangeFormat(format) log.Infof("this is a test message, %d", 1111) format = fmt.Sprintf("%s %s %s %s:%d %s", "2006-01-02", "15:04:05.999999", log.LevelToken, log.PackageToken, log.LineToken, log.MessageToken) log.ChangeFormat(format) log.Infof("this is a test message, %d", 1111) format = fmt.Sprintf("%s %s %s:%d %s", "2006-01-02 15:04:05.000000", log.LevelToken, log.ProjectToken, log.LineToken, log.MessageToken) log.ChangeFormat(format) log.Infof("this is a test message, %d", 1111) format = fmt.Sprintf(`{"date": "%s", "time": "%s", "level": "%s", "file": "%s", "line": %d, "log": "%s"}`, "2006-01-02", "15:04:05.999", log.LevelToken, log.ProjectToken, log.LineToken, log.MessageToken) log.ChangeFormat(format) log.Infof("this is a test message, %d", 1111) format = fmt.Sprintf(`<log><date>%s</date><time>%s</time><level>%s</level><file>%s</file><line>%d</line><msg>%s</msg><log>`, "2006-01-02", "15:04:05.000", log.LevelToken, log.ProjectToken, log.LineToken, log.MessageToken) log.ChangeFormat(format) log.Infof("this is a test message, %d", 1111) }
// Post 工具类, POST json 并返回 error func Post(url string, js []byte) (err error) { wxerr := &WeixinError{} err = PostUnmarshal(url, js, wxerr) if err != nil { return err } if wxerr.ErrCode == WeixinErrCodeSuccess { return nil } // if wxerr.ErrCode == WeixinErrCodeSystemBusy { // // } log.Errorf("weixin error %d: %s", wxerr.ErrCode, wxerr.ErrMsg) return wxerr }
// NewRecvEvent 把通用 struct 转化成相应类型的 struct func NewRecvEvent(msg *Message) RecvEvent { switch msg.Event { case EventTypeSubscribe: return NewEventSubscribe(msg) case EventTypeUnsubscribe: return NewEventSubscribe(msg) case EventTypeLocation: return NewEventLocation(msg) case EventTypeClick: return NewEventClick(msg) case EventTypeView: return NewEventView(msg) case EventTypeScancodePush: return NewEventScancodePush(msg) case EventTypeScancodeWaitmsg: return NewEventScancodeWaitmsg(msg) case EventTypePicSysphoto: return NewEventPicSysphoto(msg) case EventTypePicPhotoOrAlbum: return NewEventPicPhotoOrAlbum(msg) case EventTypePicWeixin: return NewEventPicWeixin(msg) case EventTypeLocationSelect: return NewEventLocationSelect(msg) case EventTypeQualificationVerifySuccess: return NewEventQualificationVerifySuccess(msg) case EventTypeQualificationVerifyFail: return NewEventQualificationVerifyFail(msg) case EventTypeNamingVerifySuccess: return NewEventNamingVerifySuccess(msg) case EventTypeNamingVerifyFail: return NewEventNamingVerifyFail(msg) case EventTypeAnnualRenew: return NewEventAnnualRenew(msg) case EventTypeVerifyExpired: return NewEventVerifyExpired(msg) default: log.Errorf("unexpected receive EventType: %s", msg.Event) return nil } }
// HandleMessage 处理各类消息 func HandleMessage(msg *Message) (ret ReplyMsg) { log.Debugf("process `%s` message", msg.MsgType) switch msg.MsgType { case MsgTypeText: if RecvTextHandler != nil { return RecvTextHandler(NewRecvText(msg)) } case MsgTypeImage: if RecvImageHandler != nil { return RecvImageHandler(NewRecvImage(msg)) } case MsgTypeVoice: if RecvVoiceHandler != nil { return RecvVoiceHandler(NewRecvVoice(msg)) } case MsgTypeVideo: if RecvVideoHandler != nil { return RecvVideoHandler(NewRecvVideo(msg)) } case MsgTypeShortVideo: if RecvShortVideoHandler != nil { return RecvShortVideoHandler(NewRecvVideo(msg)) } case MsgTypeLocation: if RecvLocationHandler != nil { return RecvLocationHandler(NewRecvLocation(msg)) } case MsgTypeLink: if RecvLinkHandler != nil { return RecvLinkHandler(NewRecvLink(msg)) } case MsgTypeEvent: return HandleEvent(msg) default: log.Errorf("unexpected receive MsgType: %s", msg.MsgType) return nil } return RecvDefaultHandler(msg) }
// NewRecvMsg 把通用 struct 转化成相应类型的 struct func NewRecvMsg(msg *Message) RecvMsg { switch msg.MsgType { case MsgTypeText: return NewRecvText(msg) case MsgTypeImage: return NewRecvImage(msg) case MsgTypeVoice: return NewRecvVoice(msg) case MsgTypeVideo: return NewRecvVideo(msg) case MsgTypeShortVideo: return NewRecvVideo(msg) case MsgTypeLocation: return NewRecvLocation(msg) case MsgTypeLink: return NewRecvLink(msg) default: log.Errorf("unexpected receive MsgType: %s", msg.MsgType) return nil } }
// HandleEvent 处理各类事件 func HandleEvent(msg *Message) (reply ReplyMsg) { log.Debugf("process `%s` event", msg.MsgType) switch msg.Event { case EventTypeSubscribe: if EventSubscribeHandler != nil { return EventSubscribeHandler(NewEventSubscribe(msg)) } case EventTypeUnsubscribe: if EventUnsubscribeHandler != nil { return EventUnsubscribeHandler(NewEventSubscribe(msg)) } case EventTypeLocation: if EventLocationHandler != nil { return EventLocationHandler(NewEventLocation(msg)) } case EventTypeClick: if EventClickHandler != nil { return EventClickHandler(NewEventClick(msg)) } case EventTypeView: if EventViewHandler != nil { return EventViewHandler(NewEventView(msg)) } case EventTypeTemplateSendJobFinish: if EventTemplateSendJobFinishHandler != nil { return EventTemplateSendJobFinishHandler(NewEventTemplateSendJobFinish(msg)) } case EventTypeScancodePush: if EventScancodePushHandler != nil { return EventScancodePushHandler(NewEventScancodePush(msg)) } case EventTypeScancodeWaitmsg: if EventScancodeWaitmsgHandler != nil { return EventScancodeWaitmsgHandler(NewEventScancodeWaitmsg(msg)) } case EventTypePicSysphoto: if EventPicSysphotoHandler != nil { return EventPicSysphotoHandler(NewEventPicSysphoto(msg)) } case EventTypePicPhotoOrAlbum: if EventPicPhotoOrAlbumHandler != nil { return EventPicPhotoOrAlbumHandler(NewEventPicPhotoOrAlbum(msg)) } case EventTypePicWeixin: if EventPicWeixinHandler != nil { return EventPicWeixinHandler(NewEventPicWeixin(msg)) } case EventTypeLocationSelect: if EventLocationSelectHandler != nil { return EventLocationSelectHandler(NewEventLocationSelect(msg)) } case EventTypeQualificationVerifySuccess: if EventQualificationVerifySuccessHandler != nil { return EventQualificationVerifySuccessHandler(NewEventQualificationVerifySuccess(msg)) } case EventTypeQualificationVerifyFail: if EventQualificationVerifyFailHandler != nil { return EventQualificationVerifyFailHandler(NewEventQualificationVerifyFail(msg)) } case EventTypeNamingVerifySuccess: if EventNamingVerifySuccessHandler != nil { return EventNamingVerifySuccessHandler(NewEventNamingVerifySuccess(msg)) } case EventTypeNamingVerifyFail: if EventNamingVerifyFailHandler != nil { return EventNamingVerifyFailHandler(NewEventNamingVerifyFail(msg)) } case EventTypeAnnualRenew: if EventAnnualRenewHandler != nil { return EventAnnualRenewHandler(NewEventAnnualRenew(msg)) } case EventTypeVerifyExpired: if EventVerifyExpiredHandler != nil { return EventVerifyExpiredHandler(NewEventVerifyExpired(msg)) } default: log.Errorf("unexpected receive EventType: %s", msg.Event) return nil } return EventDefaultHandler(msg) }