diff --git a/queue/attempt.go b/queue/attempt.go index 16c7226..4323e49 100644 --- a/queue/attempt.go +++ b/queue/attempt.go @@ -88,7 +88,7 @@ type Attempt struct { } func NewAttempt(ctx context.Context, member *model.MemberAttempt, log *wlog.Logger) *Attempt { - return &Attempt{ + a := &Attempt{ state: model.MemberStateIdle, member: member, Context: ctx, @@ -100,6 +100,13 @@ func NewAttempt(ctx context.Context, member *model.MemberAttempt, log *wlog.Logg wlog.String("name", member.Name), ), } + if member.MemberId != nil { + a.log = a.log.With( + wlog.Int64("member_id", *member.MemberId), + ) + } + + return a } // Change attempt settings diff --git a/queue/call_agent.go b/queue/call_agent.go index 0fbd2d7..e1612d1 100644 --- a/queue/call_agent.go +++ b/queue/call_agent.go @@ -106,7 +106,9 @@ top: if agentCall.TransferTo() != nil && agentCall.TransferToAgentId() != nil && agentCall.TransferFromAttemptId() != nil { attempt.Log("receive transfer queue") if nc, err := queue.GetTransferredCall(*agentCall.TransferTo()); err != nil { - wlog.Error(err.Error()) + attempt.log.Error(err.Error(), + wlog.Err(err), + ) } else { if nc.HangupAt() == 0 { if newA, err := queue.queueManager.TransferFrom(team, attempt, *agentCall.TransferFromAttemptId(), @@ -114,7 +116,9 @@ top: agent = newA attempt.Log(fmt.Sprintf("transfer call from [%s] to [%s] AGENT_ID = %s {%d, %d}", agentCall.Id(), nc.Id(), newA.Name(), attempt.Id(), *agentCall.TransferFromAttemptId())) } else { - wlog.Error(err.Error()) + attempt.log.Error(err.Error(), + wlog.Err(err), + ) } agentCall = nc @@ -155,7 +159,7 @@ top: } if agentCall != nil && agentCall.HangupAt() == 0 { - wlog.Warn(fmt.Sprintf("agent call %s no hangup", agentCall.Id())) + attempt.log.Warn(fmt.Sprintf("agent call %s no hangup", agentCall.Id())) } if agentCall != nil && agentCall.BridgeAt() > 0 { diff --git a/queue/call_inbound.go b/queue/call_inbound.go index 059f802..42a1516 100644 --- a/queue/call_inbound.go +++ b/queue/call_inbound.go @@ -53,7 +53,9 @@ func (queue *InboundQueue) run(attempt *Attempt, mCall call_manager.Call) { attempt.SetState(model.MemberStateJoined) attempt.Log("wait agent") if err = queue.queueManager.SetFindAgentState(attempt.Id()); err != nil { - wlog.Error(err.Error()) + attempt.log.Error(err.Error(), + wlog.Err(err), + ) //todo return } @@ -98,14 +100,18 @@ func (queue *InboundQueue) run(attempt *Attempt, mCall call_manager.Call) { calling = false break } else { - wlog.Debug(fmt.Sprintf("[%d] change call state to %s", attempt.Id(), c)) + attempt.log.Debug(fmt.Sprintf("[%d] change call state to %s", attempt.Id(), c), + wlog.String("state", c.String()), + ) } case <-ags: agent = attempt.Agent() team, err = queue.GetTeam(attempt) if err != nil { - wlog.Error(err.Error()) // todo + attempt.log.Error(err.Error(), + wlog.Err(err), + ) // todo time.Sleep(time.Second * 3) continue } @@ -143,7 +149,7 @@ func (queue *InboundQueue) run(attempt *Attempt, mCall call_manager.Call) { team.Distribute(queue, agent, NewDistributeEvent(attempt, agent.UserId(), queue, agent, queue.Processing(), mCall, agentCall)) agentCall.Invite() - wlog.Debug(fmt.Sprintf("call [%s] && agent [%s]", mCall.Id(), agentCall.Id())) + attempt.log.Debug(fmt.Sprintf("call [%s] && agent [%s]", mCall.Id(), agentCall.Id())) top: for calling && agentCall.HangupCause() == "" && (mCall.HangupCause() == "") { @@ -200,7 +206,9 @@ func (queue *InboundQueue) run(attempt *Attempt, mCall call_manager.Call) { if agentCall.TransferTo() != nil && agentCall.TransferToAgentId() != nil && agentCall.TransferFromAttemptId() != nil { attempt.Log("receive transfer queue") if nc, err := queue.GetTransferredCall(*agentCall.TransferTo()); err != nil { - wlog.Error(err.Error()) + attempt.log.Error(err.Error(), + wlog.Err(err), + ) } else { if nc.HangupAt() == 0 { if newA, err := queue.queueManager.TransferFrom(team, attempt, *agentCall.TransferFromAttemptId(), @@ -208,7 +216,9 @@ func (queue *InboundQueue) run(attempt *Attempt, mCall call_manager.Call) { agent = newA attempt.Log(fmt.Sprintf("transfer call from [%s] to [%s] AGENT_ID = %s {%d, %d}", agentCall.Id(), nc.Id(), newA.Name(), attempt.Id(), *agentCall.TransferFromAttemptId())) } else { - wlog.Error(err.Error()) + attempt.log.Error(err.Error(), + wlog.Err(err), + ) } agentCall = nc @@ -265,7 +275,7 @@ func (queue *InboundQueue) run(attempt *Attempt, mCall call_manager.Call) { } if agentCall != nil && agentCall.HangupAt() == 0 { - wlog.Warn(fmt.Sprintf("agent call %s no hangup", agentCall.Id())) + attempt.log.Warn(fmt.Sprintf("agent call %s no hangup", agentCall.Id())) } if agentCall != nil && agentCall.BridgeAt() > 0 { @@ -277,7 +287,9 @@ func (queue *InboundQueue) run(attempt *Attempt, mCall call_manager.Call) { if mCall.HangupAt() == 0 && mCall.BridgeAt() == 0 { err = mCall.StopPlayback() if err != nil { - wlog.Error(err.Error()) + attempt.log.Error(err.Error(), + wlog.Err(err), + ) } } diff --git a/queue/call_ivr.go b/queue/call_ivr.go index 3352cd1..429faa4 100644 --- a/queue/call_ivr.go +++ b/queue/call_ivr.go @@ -154,10 +154,13 @@ func (queue *IVRQueue) run(attempt *Attempt) { call.Invite() if call.Err() != nil { + // TODO return } - wlog.Debug(fmt.Sprintf("calling %s for member %s attemptId %v", call.Id(), attempt.Name(), attempt.Id())) + attempt.log.Debug(fmt.Sprintf("calling %s for member %s attemptId %v", call.Id(), attempt.Name(), attempt.Id()), + wlog.String("call_id", call.Id()), + ) var calling = true @@ -175,7 +178,9 @@ func (queue *IVRQueue) run(attempt *Attempt) { _, err := queue.queueManager.store.Member(). SetAttemptOffering(attempt.Id(), nil, nil, model.NewString(call.Id()), &dst, &callerIdNumber) if err != nil { - wlog.Error(err.Error()) + attempt.log.Error(err.Error(), + wlog.Err(err), + ) } case call_manager.CALL_STATE_DETECT_AMD, call_manager.CALL_STATE_ACCEPT: @@ -188,7 +193,9 @@ func (queue *IVRQueue) run(attempt *Attempt) { attempt.SetState(model.MemberStateBridged) _, err := queue.queueManager.store.Member().SetAttemptBridged(attempt.Id()) if err != nil { - wlog.Error(err.Error()) + attempt.log.Error(err.Error(), + wlog.Err(err), + ) } case call_manager.CALL_STATE_HANGUP: diff --git a/queue/call_offline.go b/queue/call_offline.go index 40293f1..8bca916 100644 --- a/queue/call_offline.go +++ b/queue/call_offline.go @@ -6,7 +6,6 @@ import ( "github.com/webitel/call_center/agent_manager" "github.com/webitel/call_center/call_manager" "github.com/webitel/call_center/model" - "github.com/webitel/wlog" ) type OfflineQueueSettings struct { @@ -188,7 +187,7 @@ func (queue *OfflineCallQueue) run(team *agentTeam, attempt *Attempt, agent agen } if call.BillSeconds() > 0 || call.AcceptAt() > 0 { //FIXME Accept or Bridge ? - wlog.Debug(fmt.Sprintf("attempt[%d] reporting...", attempt.Id())) + attempt.log.Debug(fmt.Sprintf("attempt[%d] reporting...", attempt.Id())) team.Reporting(queue, attempt, agent, call.ReportingAt() > 0, call.Transferred()) } else { team.Missed(attempt, agent) diff --git a/queue/call_preview.go b/queue/call_preview.go index 2c53693..684997c 100644 --- a/queue/call_preview.go +++ b/queue/call_preview.go @@ -231,14 +231,18 @@ func (queue *PreviewCallQueue) run(team *agentTeam, attempt *Attempt, agent agen if call.TransferTo() != nil && call.TransferToAgentId() != nil && call.TransferFromAttemptId() != nil { attempt.Log("receive transfer") if nc, err := queue.GetTransferredCall(*call.TransferTo()); err != nil { - wlog.Error(err.Error()) + attempt.log.Error(err.Error(), + wlog.Err(err), + ) } else { if nc.HangupAt() == 0 { if newA, err := queue.queueManager.TransferFrom(team, attempt, *call.TransferFromAttemptId(), *call.TransferToAgentId(), *call.TransferTo(), nc); err == nil { agent = newA attempt.Log(fmt.Sprintf("transfer call from [%s] to [%s] AGENT_ID = %s {%d, %d}", call.Id(), nc.Id(), newA.Name(), attempt.Id(), *call.TransferFromAttemptId())) } else { - wlog.Error(err.Error()) + attempt.log.Error(err.Error(), + wlog.Err(err), + ) } call = nc diff --git a/queue/call_progressive.go b/queue/call_progressive.go index 728cb48..1d455ca 100644 --- a/queue/call_progressive.go +++ b/queue/call_progressive.go @@ -221,7 +221,10 @@ func (queue *ProgressiveCallQueue) run(attempt *Attempt, team *agentTeam, agent } printfIfErr(agentCall.Invite()) - wlog.Debug(fmt.Sprintf("call [%s] && agent [%s]", mCall.Id(), agentCall.Id())) + attempt.log.Debug(fmt.Sprintf("call [%s] && agent [%s]", mCall.Id(), agentCall.Id()), + wlog.String("call_id", mCall.Id()), + wlog.String("agent_call_id", agentCall.Id()), + ) top: for agentCall.HangupCause() == "" && mCall.HangupCause() == "" && agentCall.TransferTo() == nil { @@ -266,7 +269,9 @@ func (queue *ProgressiveCallQueue) run(attempt *Attempt, team *agentTeam, agent if agentCall.TransferTo() != nil && agentCall.TransferToAgentId() != nil && agentCall.TransferFromAttemptId() != nil { attempt.Log("receive transfer") if nc, err := queue.GetTransferredCall(*agentCall.TransferTo()); err != nil { - wlog.Error(err.Error()) + attempt.log.Error(err.Error(), + wlog.Err(err), + ) } else { if nc.HangupAt() == 0 { if newA, err := queue.queueManager.TransferFrom(team, attempt, *agentCall.TransferFromAttemptId(), *agentCall.TransferToAgentId(), *agentCall.TransferTo(), nc); err == nil { @@ -274,7 +279,9 @@ func (queue *ProgressiveCallQueue) run(attempt *Attempt, team *agentTeam, agent attempt.Log(fmt.Sprintf("transfer call from [%s] to [%s] AGENT_ID = %s {%d, %d}", agentCall.Id(), nc.Id(), newA.Name(), attempt.Id(), *agentCall.TransferFromAttemptId())) //transferred = true } else { - wlog.Error(err.Error()) + attempt.log.Error(err.Error(), + wlog.Err(err), + ) } agentCall = nc @@ -332,7 +339,7 @@ func (queue *ProgressiveCallQueue) run(attempt *Attempt, team *agentTeam, agent queue.queueManager.LeavingMember(attempt) } else { if agentCall.BridgeAt() > 0 { //FIXME Accept or Bridge ? - wlog.Debug(fmt.Sprintf("attempt[%d] reporting...", attempt.Id())) + attempt.log.Debug(fmt.Sprintf("attempt[%d] reporting...", attempt.Id())) team.Reporting(queue, attempt, agent, agentCall.ReportingAt() > 0, agentCall.Transferred()) } else { if agentCall.HangupAt() == 0 && agentCall.TransferTo() == nil && mCall.HangupAt() > 0 { diff --git a/queue/chat_inbound.go b/queue/chat_inbound.go index 92cc7cf..280e468 100644 --- a/queue/chat_inbound.go +++ b/queue/chat_inbound.go @@ -131,7 +131,9 @@ func (queue *InboundChatQueue) process(attempt *Attempt, inviterId, invUserId st agent = attempt.Agent() team, err = queue.GetTeam(attempt) if err != nil { - wlog.Error(err.Error()) + attempt.log.Error(err.Error(), + wlog.Err(err), + ) return } attempt.Log(fmt.Sprintf("distribute agent %s [%d]", agent.Name(), agent.Id())) @@ -177,7 +179,9 @@ func (queue *InboundChatQueue) process(attempt *Attempt, inviterId, invUserId st aSess = conv.LastSession() team.Distribute(queue, agent, NewDistributeEvent(attempt, agent.UserId(), queue, agent, queue.Processing(), mSess, aSess)) - wlog.Debug(fmt.Sprintf("conversation [%s] && agent [%s]", conv.MemberSession().Id(), conv.LastSession().Id())) + attempt.log.Debug(fmt.Sprintf("conversation [%s] && agent [%s]", conv.MemberSession().Id(), conv.LastSession().Id()), + wlog.String("conversation_id", conv.MemberSession().Id()), + ) top: for conv.Active() && aSess.StopAt() == 0 { // diff --git a/queue/dialing.go b/queue/dialing.go index 1c33fff..2fe8745 100644 --- a/queue/dialing.go +++ b/queue/dialing.go @@ -86,19 +86,25 @@ func (d *DialingImpl) routeIdleAttempts() { err = d.queueManager.mq.AgentChannelEvent(v.Channel, v.DomainId, 0, v.UserId, waiting) } } else { - wlog.Error(err.Error()) ///TODO return ? + d.log.Error(err.Error(), + wlog.Err(err), + ) ///TODO return ? } members, err := d.store.Member().GetActiveMembersAttempt(d.app.GetInstanceId()) if err != nil { - wlog.Error(err.Error()) + d.log.Error(err.Error(), + wlog.Err(err), + ) time.Sleep(time.Second) return } for _, v := range members { if v.MemberId == nil { - wlog.Warn(fmt.Sprintf("Attempt=%d is canceled", v.Id)) + d.log.Warn(fmt.Sprintf("Attempt=%d is canceled", v.Id), + wlog.Int64("attempt_id", v.Id), + ) continue } v.CreatedAt = time.Now() @@ -130,11 +136,13 @@ func (d *DialingImpl) routeIdleAgents() { } else { // TODO d.queueManager.store.Member().SetTimeoutError(v.AttemptId) - wlog.Error("attempt[%d] error: not found in cache, set timeout error") + d.log.Error("attempt[%d] error: not found in cache, set timeout error") } } } else { - wlog.Error(err.Error()) ///TODO return ? + d.log.Error(err.Error(), + wlog.Err(err), + ) ///TODO return ? } // FIXME engine if hists, err := d.store.Member().SaveToHistory(); err == nil { diff --git a/queue/expired.go b/queue/expired.go index 33bff2d..ce1d40e 100644 --- a/queue/expired.go +++ b/queue/expired.go @@ -24,11 +24,13 @@ type ExpiredManager struct { watcher *utils.Watcher startOnce sync.Once pool *utils.Pool + log *wlog.Logger } type ExpiredJob struct { app App model.ExpiredMember + log *wlog.Logger } func NewExpiredManager(app App, store store.Store) *ExpiredManager { @@ -36,11 +38,15 @@ func NewExpiredManager(app App, store store.Store) *ExpiredManager { manager.app = app manager.store = store manager.pool = utils.NewPool(ExpiredWorkers, ExpiredQueue) + manager.log = wlog.GlobalLogger().With( + wlog.Namespace("context"), + wlog.String("name", "expired_manager"), + ) return &manager } func (s *ExpiredManager) Start() { - wlog.Debug("starting expired service") + s.log.Debug("starting expired service") s.watcher = utils.MakeWatcher("Expired", ExpiredPollingInterval, s.job) s.startOnce.Do(func() { go s.watcher.Start() @@ -54,14 +60,20 @@ func (s *ExpiredManager) Stop() { func (s *ExpiredManager) job() { st := time.Now() if hooks, err := s.store.Member().SetExpired(ExpiredLimit); err != nil { - wlog.Error(err.Error()) + s.log.Error(err.Error(), + wlog.Err(err), + ) } else { - wlog.Debug(fmt.Sprintf("set expired members time: %s, hook count %d", time.Now().Sub(st), len(hooks))) + s.log.Debug(fmt.Sprintf("set expired members time: %s, hook count %d", time.Now().Sub(st), len(hooks))) for _, v := range hooks { s.pool.Exec(&ExpiredJob{ app: s.app, ExpiredMember: *v, + log: s.log.With( + wlog.Int64("member_id", v.MemberId), + wlog.Any("schema_id", v.SchemaId), + ), }) } @@ -80,8 +92,11 @@ func (v *ExpiredJob) Execute() { } if id, err := v.app.FlowManager().Queue().StartFlow(req); err != nil { - wlog.Error(fmt.Sprintf("hook \"leaving\" expired (time %s) member_id=%d, error: %s", time.Now().Sub(st), v.MemberId, err.Error())) + v.log.Error(fmt.Sprintf("hook \"leaving\" expired (time %s) member_id=%d, error: %s", time.Now().Sub(st), + v.MemberId, err.Error()), + wlog.Err(err), + ) } else { - wlog.Debug(fmt.Sprintf("hook \"leaving\" expired (time %s) member_id=%d, job_id: %s", time.Now().Sub(st), v.MemberId, id)) + v.log.Debug(fmt.Sprintf("hook \"leaving\" expired (time %s) member_id=%d, job_id: %s", time.Now().Sub(st), v.MemberId, id)) } } diff --git a/queue/manual.go b/queue/manual.go index c26084a..8f441cc 100644 --- a/queue/manual.go +++ b/queue/manual.go @@ -31,14 +31,18 @@ func (qm *Manager) stopWaitingList() { func (qm *Manager) listWaiting() { list, err := qm.store.Member().WaitingList() if err != nil { - wlog.Error(err.Error()) + qm.log.Error(err.Error(), + wlog.Err(err), + ) return } for _, v := range list { err = qm.app.NotificationWaitingList(v) if err != nil { - wlog.Error(err.Error()) + qm.log.Error(err.Error(), + wlog.Err(err), + ) } } } diff --git a/queue/queue.go b/queue/queue.go index 51732d4..beebd66 100644 --- a/queue/queue.go +++ b/queue/queue.go @@ -43,6 +43,7 @@ type QueueObject interface { AutoAnswerValue() interface{} RingtoneUri() string AmdPlaybackUri() *string // todo move to amd + Log() *wlog.Logger } type BaseQueue struct { @@ -69,6 +70,7 @@ type BaseQueue struct { endless bool hooks HookHub amdPlaybackFileUri *string + log *wlog.Logger } func NewBaseQueue(queueManager *Manager, resourceManager *ResourceManager, settings *model.Queue) BaseQueue { @@ -93,6 +95,11 @@ func NewBaseQueue(queueManager *Manager, resourceManager *ResourceManager, setti processingRenewalSec: settings.ProcessingRenewalSec, endless: settings.Endless, hooks: NewHookHub(settings.Hooks), + log: queueManager.log.With( + wlog.Int("queue_id", settings.Id), + wlog.Int64("domain_id", settings.DomainId), + wlog.String("channel", settings.Channel()), + ), } if settings.RingtoneId != nil && settings.RingtoneType != nil { @@ -186,6 +193,15 @@ func (queue *BaseQueue) HasForm() bool { return queue.formSchemaId != nil && queue.Processing() } +func (queue *BaseQueue) Log() *wlog.Logger { + if queue.log == nil { + queue.log = wlog.GlobalLogger() // TODO + queue.log.Error("not found logger") + } + + return queue.log +} + func (queue *BaseQueue) Manager() *Manager { return queue.queueManager } @@ -357,7 +373,10 @@ func (queue *BaseQueue) Leaving(attempt *Attempt) { func (tm *agentTeam) Distribute(queue QueueObject, agent agent_manager.AgentObject, e model.Event) { if err := tm.teamManager.mq.AgentChannelEvent(queue.Channel(), queue.DomainId(), queue.Id(), agent.UserId(), e); err != nil { - wlog.Error(err.Error()) + agent.Log().Error(err.Error(), + wlog.Err(err), + wlog.Int("queue_id", queue.Id()), + ) } } @@ -371,14 +390,18 @@ func (tm *agentTeam) Offering(attempt *Attempt, agent agent_manager.AgentObject, timestamp, err := tm.teamManager.store.Member().SetAttemptOffering(attempt.Id(), agentId, agentCallId, mCallId, &attempt.communication.Destination, attempt.communication.Display) if err != nil { - wlog.Error(err.Error()) + attempt.log.Error(err.Error(), + wlog.Err(err), + ) return } attempt.SetState(model.MemberStateOffering) e := NewOfferingEvent(attempt, agent.UserId(), timestamp, aChannel, mChannel) err = tm.teamManager.mq.AgentChannelEvent(attempt.channel, attempt.domainId, attempt.QueueId(), agent.UserId(), e) if err != nil { - wlog.Error(err.Error()) + attempt.log.Error(err.Error(), + wlog.Err(err), + ) return } } @@ -394,7 +417,9 @@ func (tm *agentTeam) Cancel(attempt *Attempt, agent agent_manager.AgentObject) { attempt.maxAttempts, attempt.waitBetween, nil, attempt.perNumbers, attempt.excludeCurrNumber, attempt.redial, attempt.description, attempt.stickyAgentId) if err != nil { - wlog.Error(err.Error()) + attempt.log.Error(err.Error(), + wlog.Err(err), + ) return } @@ -407,7 +432,9 @@ func (tm *agentTeam) Cancel(attempt *Attempt, agent agent_manager.AgentObject) { e := NewWaitingChannelEvent(attempt.channel, agent.UserId(), attId, res.Timestamp) err = tm.teamManager.mq.AgentChannelEvent(attempt.channel, attempt.domainId, attempt.QueueId(), agent.UserId(), e) if err != nil { - wlog.Error(err.Error()) + attempt.log.Error(err.Error(), + wlog.Err(err), + ) } } diff --git a/queue/queue_manager.go b/queue/queue_manager.go index a1b91c0..a719474 100644 --- a/queue/queue_manager.go +++ b/queue/queue_manager.go @@ -84,7 +84,7 @@ func (qm *Manager) Start() { qm.log.Debug("queueManager started") defer func() { - wlog.Debug("stopped QueueManager") + qm.log.Debug("stopped QueueManager") close(qm.stopped) }() @@ -220,7 +220,7 @@ func (qm *Manager) GetQueue(id int, updatedAt int64) (QueueObject, *model.AppErr } qm.queuesCache.AddWithDefaultExpires(id, queue) - wlog.Debug(fmt.Sprintf("add queue %s to cache", queue.Name())) + queue.Log().Debug(fmt.Sprintf("add queue %s to cache", queue.Name())) return queue, nil } @@ -230,18 +230,23 @@ func (qm *Manager) GetResource(id, updatedAt int64) (ResourceObject, *model.AppE func (qm *Manager) SetResourceError(resource ResourceObject, errorId string) { if resource.CheckCodeError(errorId) { - wlog.Warn(fmt.Sprintf("resource %s Id=%d error: %s", resource.Name(), resource.Id(), errorId)) + resource.Log().Warn(fmt.Sprintf("resource %s Id=%d error: %s", resource.Name(), resource.Id(), errorId), + wlog.String("error_id", errorId), + ) + if responseError, err := qm.store.OutboundResource(). SetError(int64(resource.Id()), int64(1), errorId, model.OUTBOUND_RESOURCE_STRATEGY_RANDOM); err != nil { - wlog.Error(err.Error()) + resource.Log().Error(err.Error(), + wlog.Err(err), + ) } else { if responseError.Stopped != nil && *responseError.Stopped { - wlog.Info(fmt.Sprintf("resource %s [%d] stopped, because: %s", resource.Name(), resource.Id(), errorId)) + resource.Log().Info(fmt.Sprintf("resource %s [%d] stopped, because: %s", resource.Name(), resource.Id(), errorId)) } if responseError.UnReserveResourceId != nil { - wlog.Info(fmt.Sprintf("new resource ResourceId=%d from reserve", *responseError.UnReserveResourceId)) + resource.Log().Info(fmt.Sprintf("new resource ResourceId=%d from reserve", *responseError.UnReserveResourceId)) } qm.resourceManager.RemoveFromCacheById(int64(resource.Id())) } @@ -251,7 +256,9 @@ func (qm *Manager) SetResourceError(resource ResourceObject, errorId string) { func (qm *Manager) SetResourceSuccessful(resource ResourceObject) { if resource.SuccessivelyErrors() > 0 { if err := qm.store.OutboundResource().SetSuccessivelyErrorsById(int64(resource.Id()), 0); err != nil { - wlog.Error(err.Error()) + resource.Log().Error(err.Error(), + wlog.Err(err), + ) } } } @@ -270,7 +277,9 @@ func (qm *Manager) DistributeAttempt(attempt *Attempt) (QueueObject, *model.AppE queue, err := qm.GetQueue(attempt.QueueId(), attempt.QueueUpdatedAt()) if err != nil { - wlog.Error(err.Error()) + attempt.log.Error(err.Error(), + wlog.Err(err), + ) //TODO added to model "dialing.queue.new_queue.app_error" //panic(err.Error()) return nil, err @@ -283,7 +292,9 @@ func (qm *Manager) DistributeAttempt(attempt *Attempt) (QueueObject, *model.AppE if attempt.IsBarred() { err = qm.Barred(attempt) if err != nil { - wlog.Error(err.Error()) + attempt.log.Error(err.Error(), + wlog.Err(err), + ) } else { attempt.Log("this destination is barred") } @@ -300,13 +311,15 @@ func (qm *Manager) DistributeAttempt(attempt *Attempt) (QueueObject, *model.AppE attempt.resource = qm.GetAttemptResource(attempt) if err = queue.DistributeAttempt(attempt); err != nil { - wlog.Error(err.Error()) + attempt.log.Error(err.Error(), + wlog.Err(err), + ) qm.Abandoned(attempt) qm.LeavingMember(attempt) return nil, err } else { - wlog.Info(fmt.Sprintf("[%s] join member %s[%v] AttemptId=%d to queue \"%s\" (size %d, waiting %d, active %d)", queue.TypeName(), attempt.Name(), + attempt.log.Info(fmt.Sprintf("[%s] join member %s[%v] AttemptId=%d to queue \"%s\" (size %d, waiting %d, active %d)", queue.TypeName(), attempt.Name(), attempt.MemberId(), attempt.Id(), queue.Name(), attempt.member.QueueCount, attempt.member.QueueWaitingCount, attempt.member.QueueActiveCount)) } @@ -339,7 +352,9 @@ func (qm *Manager) DistributeCall(_ context.Context, in *cc.CallJoinToQueueReque ) if err != nil { - wlog.Error(fmt.Sprintf("[inbound] join member call_id %s queue %d, %s", in.GetMemberCallId(), in.GetQueue().GetId(), err.Error())) + qm.log.Error(fmt.Sprintf("[inbound] join member call_id %s queue %d, %s", in.GetMemberCallId(), in.GetQueue().GetId(), err.Error()), + wlog.Err(err), + ) return nil, err } @@ -370,7 +385,11 @@ func (qm *Manager) DistributeCall(_ context.Context, in *cc.CallJoinToQueueReque } if q, err = qm.GetQueue(res.QueueId, res.QueueUpdatedAt); err != nil { - wlog.Error(fmt.Sprintf("[inbound] join member call_id %s queue %d, %s", in.GetMemberCallId(), in.GetQueue().GetId(), err.Error())) + qm.log.Error(fmt.Sprintf("[inbound] join member call_id %s queue %d, %s", in.GetMemberCallId(), in.GetQueue().GetId(), err.Error()), + wlog.Err(err), + wlog.Int64("attempt_id", res.AttemptId), + wlog.Int("queue_id", res.QueueId), + ) return nil, err } @@ -383,7 +402,11 @@ func (qm *Manager) DistributeCall(_ context.Context, in *cc.CallJoinToQueueReque _, err = qm.callManager.InboundCallQueue(callInfo, ringtone, q.Variables()) if err != nil { printfIfErr(qm.store.Member().DistributeCallToQueueCancel(res.AttemptId)) - wlog.Error(fmt.Sprintf("[%s] call %s (%d) distribute error: %s", callInfo.AppId, callInfo.Id, res.AttemptId, err.Error())) + qm.log.Error(fmt.Sprintf("[%s] call %s (%d) distribute error: %s", callInfo.AppId, callInfo.Id, res.AttemptId, err.Error()), + wlog.Err(err), + wlog.Int64("attempt_id", res.AttemptId), + wlog.Int("queue_id", res.QueueId), + ) return nil, err } @@ -445,20 +468,26 @@ func (qm *Manager) DistributeCallToAgent(ctx context.Context, in *cc.CallJoinToA ) if err != nil { - wlog.Error(err.Error()) + qm.log.Error(err.Error(), + wlog.Err(err), + ) return nil, err } if in.CancelDistribute { err = qm.CancelAgentDistribute(in.GetAgentId()) if err != nil { - wlog.Error(err.Error()) + qm.log.Error(err.Error(), + wlog.Err(err), + ) } } agent, err = qm.agentManager.GetAgent(int(in.GetAgentId()), res.AgentUpdatedAt) if err != nil { - wlog.Error(err.Error()) + qm.log.Error(err.Error(), + wlog.Err(err), + ) return nil, err } @@ -491,7 +520,10 @@ func (qm *Manager) DistributeCallToAgent(ctx context.Context, in *cc.CallJoinToA _, err = qm.callManager.ConnectCall(callInfo, ringtone) if err != nil { printfIfErr(qm.store.Member().DistributeCallToQueueCancel(res.AttemptId)) - wlog.Error(fmt.Sprintf("[%s] call %s (%d) distribute error: %s", callInfo.AppId, callInfo.Id, res.AttemptId, err.Error())) + qm.log.Error(fmt.Sprintf("[%s] call %s (%d) distribute error: %s", callInfo.AppId, callInfo.Id, res.AttemptId, err.Error()), + wlog.Err(err), + wlog.Int64("attempt_id", res.AttemptId), + ) return nil, err } @@ -543,13 +575,15 @@ func (qm *Manager) DistributeCallToAgent(ctx context.Context, in *cc.CallJoinToA attempt.channel = model.QueueChannelCall if err = queue.DistributeAttempt(attempt); err != nil { - wlog.Error(err.Error()) + attempt.log.Error(err.Error(), + wlog.Err(err), + ) qm.Abandoned(attempt) qm.LeavingMember(attempt) return nil, err } else { - wlog.Info(fmt.Sprintf("[%s] join member %s[%v] AttemptId=%d to queue \"%s\" (size %d, waiting %d, active %d)", queue.TypeName(), attempt.Name(), + attempt.log.Info(fmt.Sprintf("[%s] join member %s[%v] AttemptId=%d to queue \"%s\" (size %d, waiting %d, active %d)", queue.TypeName(), attempt.Name(), attempt.MemberId(), attempt.Id(), queue.Name(), attempt.member.QueueCount, attempt.member.QueueWaitingCount, attempt.member.QueueActiveCount)) } @@ -588,20 +622,26 @@ func (qm *Manager) DistributeTaskToAgent(ctx context.Context, in *cc.TaskJoinToA ) if err != nil { - wlog.Error(err.Error()) + qm.log.Error(err.Error(), + wlog.Err(err), + ) return nil, err } if in.CancelDistribute { err = qm.CancelAgentDistribute(in.GetAgentId()) if err != nil { - wlog.Error(err.Error()) + qm.log.Error(err.Error(), + wlog.Err(err), + ) } } agent, err = qm.agentManager.GetAgent(int(in.GetAgentId()), res.AgentUpdatedAt) if err != nil { - wlog.Error(err.Error()) + qm.log.Error(err.Error(), + wlog.Err(err), + ) return nil, err } @@ -654,13 +694,15 @@ func (qm *Manager) DistributeTaskToAgent(ctx context.Context, in *cc.TaskJoinToA attempt.channel = model.QueueChannelTask if err = queue.DistributeAttempt(attempt); err != nil { - wlog.Error(err.Error()) + attempt.log.Error(err.Error(), + wlog.Err(err), + ) qm.Abandoned(attempt) qm.LeavingMember(attempt) return nil, err } else { - wlog.Info(fmt.Sprintf("[%s] join member %s[%v] AttemptId=%d to queue \"%s\" (size %d, waiting %d, active %d)", queue.TypeName(), attempt.Name(), + attempt.log.Info(fmt.Sprintf("[%s] join member %s[%v] AttemptId=%d to queue \"%s\" (size %d, waiting %d, active %d)", queue.TypeName(), attempt.Name(), attempt.MemberId(), attempt.Id(), queue.Name(), attempt.member.QueueCount, attempt.member.QueueWaitingCount, attempt.member.QueueActiveCount)) } @@ -692,7 +734,9 @@ func (qm *Manager) DistributeChatToQueue(_ context.Context, in *cc.ChatJoinToQue ) if err != nil { - wlog.Error(err.Error()) + qm.log.Error(err.Error(), + wlog.Err(err), + ) return nil, err } @@ -727,16 +771,24 @@ func (qm *Manager) DistributeDirectMember(memberId int64, communicationId, agent member, err := qm.store.Member().DistributeDirect(qm.app.GetInstanceId(), memberId, communicationId-1, agentId) if err != nil { - wlog.Error(fmt.Sprintf("member %v to agent %v distribute error: %s", memberId, agentId, err.Error())) + qm.log.Error(fmt.Sprintf("member %v to agent %v distribute error: %s", memberId, agentId, err.Error()), + wlog.Err(err), + wlog.Int64("member_id", memberId), + wlog.Int("agent_id", agentId), + ) return nil, err } attempt, _ := qm.CreateAttemptIfNotExists(context.Background(), member) if _, err = qm.DistributeAttempt(attempt); err != nil { - attempt.Log(err.Error()) + attempt.log.Error(err.Error(), + wlog.Err(err), + ) } if err = qm.app.NotificationHideMember(attempt.domainId, attempt.QueueId(), attempt.MemberId(), agentId); err != nil { - attempt.Log(err.Error()) + attempt.log.Error(err.Error(), + wlog.Err(err), + ) } return attempt, nil } @@ -744,12 +796,20 @@ func (qm *Manager) DistributeDirectMember(memberId int64, communicationId, agent func (qm *Manager) InterceptAttempt(ctx context.Context, domainId int64, attemptId int64, agentId int32) *model.AppError { queueId, err := qm.store.Member().Intercept(ctx, domainId, attemptId, agentId) if err != nil { - wlog.Error(fmt.Sprintf("intercept %v to agent %v error: %s", attemptId, agentId, err.Error())) + qm.log.Error(fmt.Sprintf("intercept %v to agent %v error: %s", attemptId, agentId, err.Error()), + wlog.Err(err), + wlog.Int64("attempt_id", attemptId), + wlog.Int64("agent_id", int64(agentId)), + ) return err } if err = qm.app.NotificationInterceptAttempt(domainId, queueId, "", attemptId, agentId); err != nil { - wlog.Error(fmt.Sprintf("intercept attempt %d notification, error : %s", attemptId, err.Error())) + qm.log.Error(fmt.Sprintf("intercept attempt %d notification, error : %s", attemptId, err.Error()), + wlog.Err(err), + wlog.Int64("attempt_id", attemptId), + wlog.Int64("agent_id", int64(agentId)), + ) } return nil @@ -788,7 +848,9 @@ func (qm *Manager) TimeoutLeavingMember(attempt *Attempt) { res, err := qm.store.Member().SchemaResult(attempt.Id(), &result, maxAttempts, waitBetween, perNumbers) if err != nil { - wlog.Error(err.Error()) + attempt.log.Error(err.Error(), + wlog.Err(err), + ) return } @@ -812,13 +874,15 @@ func (qm *Manager) LeavingMember(attempt *Attempt) { if attempt.manualDistribution && attempt.bridgedAt == 0 { if err := qm.app.NotificationInterceptAttempt(attempt.domainId, attempt.QueueId(), attempt.channel, attempt.Id(), 0); err != nil { - wlog.Error(fmt.Sprintf("intercept attempt %d notification, error : %s", attempt.Id(), err.Error())) + attempt.log.Error(fmt.Sprintf("intercept attempt %d notification, error : %s", attempt.Id(), err.Error()), + wlog.Err(err), + ) } } // todo fixme: bug if offering && reporting if _, ok := qm.membersCache.Get(attempt.Id()); !ok { - wlog.Error(fmt.Sprintf("[%d] not found", attempt.Id())) + attempt.log.Error(fmt.Sprintf("[%d] not found", attempt.Id())) return } attempt.SetState(HookLeaving) @@ -826,7 +890,7 @@ func (qm *Manager) LeavingMember(attempt *Attempt) { qm.membersCache.Remove(attempt.Id()) qm.wg.Done() - wlog.Info(fmt.Sprintf("[%s] leaving member %s[%v] AttemptId=%d from queue \"%s\" [%d]", attempt.queue.TypeName(), attempt.Name(), + attempt.log.Info(fmt.Sprintf("[%s] leaving member %s[%v] AttemptId=%d from queue \"%s\" [%d]", attempt.queue.TypeName(), attempt.Name(), attempt.MemberId(), attempt.Id(), attempt.queue.Name(), qm.membersCache.Len())) } @@ -834,7 +898,9 @@ func (qm *Manager) GetAttemptResource(attempt *Attempt) ResourceObject { if attempt.ResourceId() != nil && attempt.ResourceUpdatedAt() != nil { resource, err := qm.resourceManager.Get(*attempt.ResourceId(), *attempt.ResourceUpdatedAt()) if err != nil { - wlog.Error(fmt.Sprintf("attempt resource error: %s", err.Error())) + attempt.log.Error(fmt.Sprintf("attempt resource error: %s", err.Error()), + wlog.Err(err), + ) //FIXME } else { return resource @@ -848,7 +914,9 @@ func (qm *Manager) GetAttemptAgent(attempt *Attempt) (agent_manager.AgentObject, if attempt.AgentId() != nil && attempt.AgentUpdatedAt() != nil { agent, err := qm.agentManager.GetAgent(*attempt.AgentId(), *attempt.AgentUpdatedAt()) if err != nil { - wlog.Error(fmt.Sprintf("attempt agent error: %s", err.Error())) + attempt.log.Error(fmt.Sprintf("attempt agent error: %s", err.Error()), + wlog.Err(err), + ) //FIXME } else { return agent, true @@ -902,7 +970,9 @@ func (qm *Manager) Abandoned(attempt *Attempt) { res, err := qm.store.Member().SetAttemptAbandonedWithParams(attempt.Id(), 0, 0, nil, attempt.perNumbers, attempt.excludeCurrNumber, attempt.redial, attempt.description, attempt.stickyAgentId) if err != nil { - wlog.Error(err.Error()) + attempt.log.Error(err.Error(), + wlog.Err(err), + ) } else if res.MemberStopCause != nil { attempt.SetMemberStopCause(res.MemberStopCause) } @@ -922,7 +992,9 @@ func (qm *Manager) SetAttemptSuccess(attempt *Attempt, vars map[string]string) { res, err := qm.teamManager.store.Member().SetAttemptResult(attempt.Id(), AttemptResultSuccess, "", 0, vars, attempt.maxAttempts, attempt.waitBetween, attempt.perNumbers, attempt.description, attempt.stickyAgentId) if err != nil { - wlog.Error(err.Error()) + attempt.log.Error(err.Error(), + wlog.Err(err), + ) } else { if res.MemberStopCause != nil { attempt.SetMemberStopCause(res.MemberStopCause) @@ -935,8 +1007,9 @@ func (qm *Manager) SetAttemptAbandonedWithParams(attempt *Attempt, maxAttempts u res, err := qm.store.Member().SetAttemptAbandonedWithParams(attempt.Id(), maxAttempts, sleep, vars, attempt.perNumbers, attempt.excludeCurrNumber, attempt.redial, attempt.description, attempt.stickyAgentId) if err != nil { - wlog.Error(err.Error()) - + attempt.log.Error(err.Error(), + wlog.Err(err), + ) return } if res.MemberStopCause != nil { @@ -1042,7 +1115,10 @@ func (qm *Manager) ReportingAttempt(attemptId int64, result model.AttemptCallbac result.Status = "abandoned" } - wlog.Debug(fmt.Sprintf("attempt[%d] callback: %v", attemptId, result)) + qm.log.Debug(fmt.Sprintf("attempt[%d] callback: %v", attemptId, result), + wlog.Int64("attempt_id", attemptId), + wlog.Any("result", result), + ) attempt, _ := qm.GetAttempt(attemptId) @@ -1179,7 +1255,10 @@ func (qm *Manager) TransferTo(attempt *Attempt, toAttemptId int64) { attempt.SetResult(AttemptResultAbandoned) err := qm.store.Member().TransferredTo(attempt.Id(), toAttemptId) if err != nil { - wlog.Error(err.Error()) + attempt.log.Error(err.Error(), + wlog.Err(err), + wlog.Int64("to_attempt_id", toAttemptId), + ) } // @@ -1191,12 +1270,16 @@ func (qm *Manager) TransferFrom(team *agentTeam, attempt *Attempt, toAttemptId i a, err := qm.agentManager.GetAgent(toAgentId, 0) if err != nil { // fixme - wlog.Error(err.Error()) + attempt.log.Error(err.Error(), + wlog.Err(err), + ) } if err = qm.store.Member().TransferredFrom(attempt.Id(), toAttemptId, a.Id(), toAgentSession); err != nil { //todo - wlog.Error(err.Error()) + attempt.log.Error(err.Error(), + wlog.Err(err), + ) } if attempt.agent != nil { @@ -1212,7 +1295,10 @@ func (qm *Manager) TransferFrom(team *agentTeam, attempt *Attempt, toAttemptId i func (qm *Manager) LosePredictAgent(id int) { if err := qm.store.Agent().LosePredictAttempt(id); err != nil { - wlog.Error(err.Error()) + qm.log.Error(err.Error(), + wlog.Err(err), + wlog.Int("agent_id", id), + ) } } diff --git a/queue/resource.go b/queue/resource.go index 5659d17..215956f 100644 --- a/queue/resource.go +++ b/queue/resource.go @@ -3,6 +3,7 @@ package queue import ( "github.com/webitel/call_center/model" "github.com/webitel/call_center/utils" + "github.com/webitel/wlog" "math/rand" ) @@ -21,6 +22,7 @@ type ResourceObject interface { Variables() map[string]string Take() Gateway() *model.SipGateway + Log() *wlog.Logger } //type Gateway interface { @@ -44,9 +46,10 @@ type Resource struct { gatewayId *int64 emailProfileId *int gateway model.SipGateway + log *wlog.Logger } -func NewResource(config *model.OutboundResource, gw model.SipGateway) (ResourceObject, *model.AppError) { +func NewResource(config *model.OutboundResource, gw model.SipGateway, log *wlog.Logger) (ResourceObject, *model.AppError) { r := &Resource{ id: config.Id, updatedAt: config.UpdatedAt, @@ -58,6 +61,11 @@ func NewResource(config *model.OutboundResource, gw model.SipGateway) (ResourceO variables: model.MapStringInterfaceToString(config.Variables), displayNumbers: config.DisplayNumbers, gateway: gw, + log: log.With( + wlog.String("scope", "resource"), + wlog.Int("resource_id", config.Id), + wlog.Int64("gateway_id", gw.Id), + ), } if config.ErrorIds != nil { @@ -78,6 +86,10 @@ func (r *Resource) Name() string { return r.name } +func (r *Resource) Log() *wlog.Logger { + return r.log +} + func (r *Resource) GetDisplay() string { var l = len(r.displayNumbers) if l == 0 { diff --git a/queue/resource_manager.go b/queue/resource_manager.go index 4d9d041..e84f3ee 100644 --- a/queue/resource_manager.go +++ b/queue/resource_manager.go @@ -23,6 +23,7 @@ type ResourceManager struct { resourcesCache utils.ObjectCache patternsCache utils.ObjectCache app App + log *wlog.Logger sync.Mutex } @@ -31,6 +32,10 @@ func NewResourceManager(app App) *ResourceManager { resourcesCache: utils.NewLruWithParams(SIZE_RESOURCES_CACHE, "ResourceManager", EXPIRE_CACHE_ITEM, ""), patternsCache: utils.NewLruWithParams(SIZE_RESOURCES_CACHE, "ResourcePatternCache", -1, ""), app: app, + log: wlog.GlobalLogger().With( + wlog.Namespace("context"), + wlog.String("name", "resource_manager"), + ), } } @@ -51,7 +56,7 @@ func (r *ResourceManager) Get(id int64, updatedAt int64) (ResourceObject, *model if gw, err := r.app.GetGateway(config.GatewayId); err != nil { return nil, err } else { - resource, _ := NewResource(config, *gw) + resource, _ := NewResource(config, *gw, r.log) return resource, nil } } @@ -70,7 +75,7 @@ func (r *ResourceManager) Get(id int64, updatedAt int64) (ResourceObject, *model if !shared { r.resourcesCache.AddWithDefaultExpires(id, dialResource) - wlog.Debug(fmt.Sprintf("add resource %s to cache", dialResource.Name())) + dialResource.Log().Debug(fmt.Sprintf("add resource %s to cache", dialResource.Name())) } return dialResource, nil diff --git a/queue/schema.go b/queue/schema.go index 727c18b..a771104 100644 --- a/queue/schema.go +++ b/queue/schema.go @@ -51,7 +51,9 @@ func (qm *Manager) StartProcessingForm(schemaId int, att *Attempt) { e := NewNextFormEvent(att, agent.UserId()) appErr := qm.mq.AgentChannelEvent(att.channel, att.domainId, att.QueueId(), agent.UserId(), e) if appErr != nil { - wlog.Error(appErr.Error()) + att.log.Error(appErr.Error(), + wlog.Err(err), + ) } } @@ -65,7 +67,10 @@ func (qm *Manager) AttemptProcessingActionForm(attemptId int64, action string, f func (qm *Manager) attemptProcessingActionForm(attemptId int64, action string, fields map[string]string) error { - wlog.Debug(fmt.Sprintf("attempt[%d] action form: %v (%v)", attemptId, attemptId, fields)) + qm.log.Debug(fmt.Sprintf("attempt[%d] action form: %v (%v)", attemptId, attemptId, fields), + wlog.Int64("attempt_id", attemptId), + wlog.String("action", action), + ) attempt, _ := qm.GetAttempt(attemptId) if attempt == nil { @@ -79,7 +84,7 @@ func (qm *Manager) attemptProcessingActionForm(attemptId int64, action string, f if err != nil { attempt.Log(err.Error()) attempt.processingForm = nil // todo lock - qm.store.Member().StoreFormFields(attempt.Id(), fields) + printfIfErr(qm.store.Member().StoreFormFields(attempt.Id(), fields)) } else { // todo if attempt.processingForm == nil { @@ -95,7 +100,9 @@ func (qm *Manager) attemptProcessingActionForm(attemptId int64, action string, f e := NewNextFormEvent(attempt, attempt.agent.UserId()) appErr := qm.mq.AgentChannelEvent(attempt.channel, attempt.domainId, attempt.QueueId(), attempt.agent.UserId(), e) if appErr != nil { - wlog.Error(appErr.Error()) + attempt.log.Error(appErr.Error(), + wlog.Err(appErr), + ) return appErr } } @@ -121,7 +128,9 @@ func (qm *Manager) DoDistributeSchema(queue *BaseQueue, att *Attempt) bool { if err != nil { att.Log(fmt.Sprintf("DoDistributeAttempt error=%s duration=%s", err.Error(), time.Since(st))) - wlog.Error(fmt.Sprintf("%s", err.Error())) + att.log.Error(err.Error(), + wlog.Err(err), + ) return true } @@ -133,7 +142,9 @@ func (qm *Manager) DoDistributeSchema(queue *BaseQueue, att *Attempt) bool { case *flow.DistributeAttemptResponse_Cancel_: v := res.Result.(*flow.DistributeAttemptResponse_Cancel_).Cancel if err := qm.store.Member().SetDistributeCancel(att.Id(), v.Description, v.NextDistributeSec, v.Stop, res.Variables); err != nil { - wlog.Error(fmt.Sprintf("attempt [%d] error: %s", att.Id(), err.Error())) + att.log.Error(fmt.Sprintf("attempt [%d] error: %s", att.Id(), err.Error()), + wlog.Err(err), + ) } case *flow.DistributeAttemptResponse_Confirm_: v := res.Result.(*flow.DistributeAttemptResponse_Confirm_).Confirm @@ -221,7 +232,9 @@ func (qm *Manager) AfterDistributeSchema(att *Attempt) (*model.SchemaResult, boo if err != nil { // TODO - wlog.Error(fmt.Sprintf("AfterDistributeSchema [%d] error: %s duration=%s", att.Id(), err.Error(), time.Since(st))) + att.log.Error(fmt.Sprintf("AfterDistributeSchema [%d] error: %s duration=%s", att.Id(), err.Error(), time.Since(st)), + wlog.Err(err), + ) return nil, false } diff --git a/queue/statistics.go b/queue/statistics.go index e4a7ea9..7e95051 100644 --- a/queue/statistics.go +++ b/queue/statistics.go @@ -18,23 +18,30 @@ type StatisticsManager struct { store store.Store watcher *utils.Watcher startOnce sync.Once + log *wlog.Logger } func NewStatisticsManager(store store.Store) *StatisticsManager { var manager StatisticsManager manager.store = store + manager.log = wlog.GlobalLogger().With( + wlog.Namespace("context"), + wlog.String("name", "statistics_manager"), + ) return &manager } func (s *StatisticsManager) Start() { - wlog.Debug("starting statistics service") + s.log.Debug("starting statistics service") s.watcher = utils.MakeWatcher("Statistics", STATISTICS_WATCHER_POLLING_INTERVAL, s.refresh) s.startOnce.Do(func() { ver, err := s.store.Statistic().LibVersion() if err != nil { - wlog.Error(err.Error()) + s.log.Error(err.Error(), + wlog.Err(err), + ) } - wlog.Debug(fmt.Sprintf("cc_sql version: %s", ver)) + s.log.Debug(fmt.Sprintf("cc_sql version: %s", ver)) go s.watcher.Start() }) @@ -48,29 +55,37 @@ func (s *StatisticsManager) refresh() { st := time.Now() var err *model.AppError if err = s.store.Agent().RefreshAgentPauseCauses(); err != nil { - wlog.Error(err.Error()) + s.log.Error(err.Error(), + wlog.Err(err), + ) } - wlog.Debug(fmt.Sprintf("refresh pause_cause statistics time: %s", time.Now().Sub(st))) + s.log.Debug(fmt.Sprintf("refresh pause_cause statistics time: %s", time.Now().Sub(st))) if err = s.store.Agent().RefreshAgentStatistics(); err != nil { - wlog.Error(err.Error()) + s.log.Error(err.Error(), + wlog.Err(err), + ) } - wlog.Debug(fmt.Sprintf("refresh today statistics time: %s", time.Now().Sub(st))) + s.log.Debug(fmt.Sprintf("refresh today statistics time: %s", time.Now().Sub(st))) st = time.Now() if err = s.store.Member().RefreshQueueStatsLast2H(); err != nil { - wlog.Error(err.Error()) + s.log.Error(err.Error(), + wlog.Err(err), + ) } else { - wlog.Debug(fmt.Sprintf("refresh outbound queue statistics time: %s", time.Now().Sub(st))) + s.log.Debug(fmt.Sprintf("refresh outbound queue statistics time: %s", time.Now().Sub(st))) } st = time.Now() if err = s.store.Statistic().RefreshInbound1H(); err != nil { - wlog.Error(err.Error()) + s.log.Error(err.Error(), + wlog.Err(err), + ) } else { - wlog.Debug(fmt.Sprintf("refresh inbound queue statistics time: %s", time.Now().Sub(st))) + s.log.Debug(fmt.Sprintf("refresh inbound queue statistics time: %s", time.Now().Sub(st))) } return diff --git a/queue/task_outbound.go b/queue/task_outbound.go index d48d19b..9eec8f7 100644 --- a/queue/task_outbound.go +++ b/queue/task_outbound.go @@ -52,7 +52,9 @@ func (queue *TaskOutboundQueue) run(attempt *Attempt) { _, err := queue.queueManager.store.Member().SetAttemptBridged(attempt.Id()) if err != nil { - wlog.Error(err.Error()) + attempt.log.Error(err.Error(), + wlog.Err(err), + ) } attempt.SetState(model.MemberStateBridged) diff --git a/queue/team.go b/queue/team.go index 2861fa3..9ee691d 100644 --- a/queue/team.go +++ b/queue/team.go @@ -35,6 +35,7 @@ type agentTeam struct { data *model.Team teamManager *teamManager hook HookHub + log *wlog.Logger } func NewTeam(info *model.Team, tm *teamManager) *agentTeam { @@ -42,6 +43,12 @@ func NewTeam(info *model.Team, tm *teamManager) *agentTeam { data: info, teamManager: tm, hook: NewHookHub(info.Hooks), + log: wlog.GlobalLogger().With( + wlog.Namespace("context"), + wlog.String("name", "team"), + wlog.Int64("team_id", info.Id), + wlog.Int64("domain_id", info.DomainId), + ), } } @@ -120,7 +127,7 @@ func (tm *teamManager) GetTeam(id int, updatedAt int64) (*agentTeam, *model.AppE if !shared { tm.cache.AddWithDefaultExpires(id, team) - wlog.Debug(fmt.Sprintf("team [%d] %v store to cache", team.Id(), team.Name())) + team.log.Debug(fmt.Sprintf("team [%d] %v store to cache", team.Id(), team.Name())) } return team, nil @@ -142,9 +149,11 @@ func (tm *teamManager) HookAgent(event string, agent agent_manager.AgentObject, id, err := tm.app.FlowManager().Queue().StartFlow(req) if err != nil { - wlog.Error(fmt.Sprintf("hook \"%s\", error: %s", event, err.Error())) + team.log.Error(fmt.Sprintf("hook \"%s\", error: %s", event, err.Error()), + wlog.Err(err), + ) } else { - wlog.Debug(fmt.Sprintf("hook \"%s\" external job_id: %s", event, id)) + team.log.Debug(fmt.Sprintf("hook \"%s\" external job_id: %s", event, id)) } //call_manager.DUMP(req.Variables) @@ -221,7 +230,9 @@ func (tm *agentTeam) SetWrap(queue QueueObject, attempt *Attempt, agent agent_ma e := NewWrapTimeEventEvent(attempt.channel, model.NewInt64(attempt.Id()), agent.UserId(), res.Timestamp, res.Timestamp+(int64(tm.WrapUpTime()*1000))) err = tm.teamManager.mq.AgentChannelEvent(attempt.channel, attempt.domainId, attempt.QueueId(), agent.UserId(), e) if err != nil { - wlog.Error(err.Error()) + attempt.log.Error(err.Error(), + wlog.Err(err), + ) } } else { attempt.Log(err.Error()) @@ -275,7 +286,9 @@ func (tm *agentTeam) Reporting(queue QueueObject, attempt *Attempt, agent agent_ } timestamp, err := tm.teamManager.store.Member().SetAttemptReporting(attempt.Id(), timeoutSec) if err != nil { - wlog.Error(err.Error()) + attempt.log.Error(err.Error(), + wlog.Err(err), + ) return } @@ -288,7 +301,7 @@ func (tm *agentTeam) Reporting(queue QueueObject, attempt *Attempt, agent agent_ return } - wlog.Debug(fmt.Sprintf("attempt [%d] wait callback result for agent \"%s\", timeout=%d", attempt.Id(), agent.Name(), timeoutSec)) + attempt.log.Debug(fmt.Sprintf("attempt [%d] wait callback result for agent \"%s\", timeout=%d", attempt.Id(), agent.Name(), timeoutSec)) } func (tm *agentTeam) Missed(attempt *Attempt, agent agent_manager.AgentObject) { @@ -375,9 +388,11 @@ func (tm *agentTeam) WaitingAgentAndWaitingAttempt(attempt *Attempt, agent agent func (tm *agentTeam) SetAgentMaxNoAnswer(agent agent_manager.AgentObject) { if err := tm.teamManager.app.SetAgentBreakOut(agent); err != nil { - wlog.Error(fmt.Sprintf("agent \"%s\" change to [break_out] error %s", agent.Name(), err.Error())) + agent.Log().Error(fmt.Sprintf("agent \"%s\" change to [break_out] error %s", agent.Name(), err.Error()), + wlog.Err(err), + ) } else { - wlog.Debug(fmt.Sprintf("agent \"%s\" changed status to [break_out], maximum no answers in team \"%s\"", agent.Name(), tm.Name())) + agent.Log().Debug(fmt.Sprintf("agent \"%s\" changed status to [break_out], maximum no answers in team \"%s\"", agent.Name(), tm.Name())) } }