//  Copyright(C) 2021. Huawei Technologies Co.,Ltd.  All rights reserved.

// Package hwlog provides the capability of processing Huawei log rules.
package hwlog

import (
"bytes"
"context"
"fmt"
"go.uber.org/zap"
"runtime"
"strings"
)

// printHelper helper function for log printing
func printHelper(f func(string, ...zap.Field), msg string, ctx ...context.Context) {
str := getCallerInfo(ctx...)
f(str + msg)
}

// getCallerInfo gets the caller's information
func getCallerInfo(ctx ...context.Context) string {
var deep = stackDeep
var userID interface{}
var traceID interface{}
for _, c := range ctx {
if c == nil {
deep++
continue
}
userID = c.Value(UserID)
traceID = c.Value(ReqID)
}
var funcName string
pc, codePath, codeLine, ok := runtime.Caller(deep)
if ok {
funcName = runtime.FuncForPC(pc).Name()
}
p := strings.Split(codePath, "/")
l := len(p)
if l == pathLen {
funcName = p[l-1]
} else if l > pathLen {
funcName = fmt.Sprintf("%s/%s", p[l-pathLen], p[l-1])
}
callerPath := fmt.Sprintf("%s:%d", funcName, codeLine)
goroutineID := getGoroutineID()
str := fmt.Sprintf("%-8s%s ", goroutineID, callerPath)
if userID != nil || traceID != nil {
str = fmt.Sprintf("%s{%v}-{%v} ", str, userID, traceID)
}
return str
}

// getCallerGoroutineID gets the goroutineID
func getGoroutineID() string {
b := make([]byte, bitsize, bitsize)
b = b[:runtime.Stack(b, false)]
b = bytes.TrimPrefix(b, []byte("goroutine "))
b = b[:bytes.IndexByte(b, ' ')]
return string(b)
}

hwlog--utils.go的更多相关文章

  1. 【C#公共帮助类】 Utils 10年代码,最全的系统帮助类

    为大家分享一下个人的一个Utils系统帮助类,可能有些现在有新的技术替代,自行修改哈~ 这个帮助类主要包含:对象转换处理 .分割字符串.截取字符串.删除最后结尾的一个逗号. 删除最后结尾的指定字符后的 ...

  2. python学习笔记-import utils报错

    今天遇到一个坑爹的问题,查找了半天原因,终于解决了,在此特地记录一下. 运行环境:Windows eclipse 我在eclipse中配置了python的运行环境,在eclipse中编写python代 ...

  3. Lind.DDD.Utils.HttpHelper里静态对象引出的Http超时问题

    回到目录 Lind.DDD.Utils.HttpHelper组件主要实现了对HTTP的各种操作,如Get,Post,Put和Delete,它属于最纯粹的操作,大叔把它封装的目的主要为了实现与API安全 ...

  4. Utils

    import org.apache.commons.beanutils.BeanUtils; import org.apache.commons.collections.CollectionUtils ...

  5. Spring @Autowired注解在utils静态工具类

    [转] 使用场景:在一个静态方法中,如何使用以下注入: @Autowired private ItemMapper itemMapper; @Component public class TestUt ...

  6. utils.js

    /** * //2.0检测方式(目测,测量,专用仪器测试等) function GetCheckType() { $.ajax({ url: '@Url.Action("GetCheckTy ...

  7. Perl/Nagios – Can’t locate utils.pm in @INC

    While trying to use a Nagios plugin I got an error saying that “Can’t locate utils.pm in @INC”. Foll ...

  8. 读取 java.nio.ByteBuffer 中的字符串(String) 写入方式flash.utils.ByteArray.writeUTF

    通过研究ByteArray的写入格式以及方法说明,可以发现writeUTF是先使用2位写入字符串的长度,然后在其后写入字符串编码. flash.utils.ByteArray.writeUTF(val ...

  9. Spark中常用工具类Utils的简明介绍

    <深入理解Spark:核心思想与源码分析>一书前言的内容请看链接<深入理解SPARK:核心思想与源码分析>一书正式出版上市 <深入理解Spark:核心思想与源码分析> ...

  10. 【C#公共帮助类】 Utils最全的系统帮助类

    最近闲的没事做,自己想着做一些东西,不知不觉居然在博客园找到了这么多公共类,感觉还是挺有用的,平时自己还是用到了好多,就是缺少整理,现在为大家分享一下一个Utils系统帮助类,可能有些现在有新的技术替 ...

随机推荐

  1. JDBC连接池&JDBCTemplate

    今日内容 1. 数据库连接池 2. Spring JDBC : JDBC Template 数据库连接池 1. 概念:其实就是一个容器(集合),存放数据库连接的容器. 当系统初始化好后,容器被创建,容 ...

  2. 第七十篇:Vue组件的使用

    好家伙, 1.vue的组件化开发 1.1.什么是组件? 组件是对UI结构的复用, vue是一个支持组件化开发的前端框架, vue中规定:组件的后缀名是.vue 例如:App.vue文件本质上就是一个v ...

  3. 第四十篇:Vue的生命周期(一)

    好家伙,军训结束了,回归 Vue实例的生命周期 1.什么是生命周期? 从Vue实例创建,运行到销毁期间总是伴随着各种各样的事件,这些事件,统称为生命周期. 2.什么是生命周期钩子? 生命周期函数的别称 ...

  4. python自动更新pom文件

    前言 项目越来越多,版本管理越来越麻烦,在项目上我使用 maven version 来进行版本管理.主要还是在分布式项目中模块众多的场景中使用,毕竟各个模块对外的版本需要保持统一. 关于这个插件如何使 ...

  5. 使用Docker方式部署Gitlab,Gitlab-Runner并使用Gitlab提供的CI/CD功能自动化构建SpringBoot项目

    1.Docker安装Gitlab,地址:https://www.cnblogs.com/sanduzxcvbnm/p/13814730.html 2.Docker安装Gitlab-runner,地址: ...

  6. 5.监控MySQL

    prometheus监控MySQL需要用到mysql_exporter. mysql_exporter 项目地址:https://github.com/prometheus/mysqld_export ...

  7. day44-反射03

    Java反射03 3.通过反射获取类的结构信息 3.1java.lang.Class类 getName:获取全类名 getSimpleName:获取简单类名 getFields:获取所有public修 ...

  8. esp-idf 移植 lvgl8.3.3

    一.准备材料 开发板:esp32s3 idf版本:4.4.2 lvgl:8.3.3 注意:lvgl不要选择master分支,编译失败时不好确定问题. 二.创建idf项目 方式一 通过 VSCode 创 ...

  9. 这些不知道,别说你熟悉 Spring

    大家好,这篇文章跟大家来聊下 Spring 中提供的常用扩展点.Spring SPI 机制.以及 SpringBoot 自动装配原理,重点介绍下 Spring 基于这些扩展点怎么跟配置中心(Apoll ...

  10. 华为云ubunbu部署.NetCore3.1项目(DDD商城)

    提前项目打包发布,文件传输工具Filezilla,注意是选择sftp协议,将publish文件传到/home文件夹下 第一步  .NetCoreSDK安装 微软官方的文档https://docs.mi ...