Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
H
hilo-common
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
hujiebin
hilo-common
Commits
96755b19
You need to sign in or sign up before continuing.
Commit
96755b19
authored
Feb 08, 2023
by
hujiebin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Create context.go
parent
3afe106b
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
104 additions
and
0 deletions
+104
-0
context.go
mycontext/context.go
+104
-0
No files found.
mycontext/context.go
0 → 100644
View file @
96755b19
package
mycontext
import
(
"context"
"git.hilo.cn/hilo-common/mylogrus"
"github.com/satori/go.uuid"
"github.com/sirupsen/logrus"
"strconv"
"strings"
)
const
(
TRACEID
=
"traceId"
USERID
=
"userId"
EXTERNAL_ID
=
"externalId"
CODE
=
"code"
NICK
=
"nick"
AVATAR
=
"avatar"
COUNTRY
=
"country"
EXTERNALID1
=
"externalId1"
EXTERNALID2
=
"externalId2"
MGRID
=
"mgrId"
DEVICETYPE
=
"deviceType"
DEVICEVERSION
=
"deviceVersion"
APP_VERSION
=
"appVersion"
ACTION_RESULt
=
"actionResult"
URL
=
"url"
METHOD
=
"method"
IMEI
=
"imei"
LANGUAGE
=
"language"
)
/**
* 主要是完成日志打印
* @param
* @return
**/
type
MyContext
struct
{
context
.
Context
Log
*
logrus
.
Entry
Cxt
map
[
string
]
interface
{}
}
func
CreateMyContextWith
(
traceId
interface
{})
*
MyContext
{
cxt
:=
map
[
string
]
interface
{}{}
cxt
[
TRACEID
]
=
traceId
return
CreateMyContext
(
cxt
)
}
func
CreateMyContext
(
ctx
map
[
string
]
interface
{})
*
MyContext
{
var
traceId
string
if
traceIdTemp
,
ok
:=
ctx
[
TRACEID
];
ok
{
traceId
,
ok
=
traceIdTemp
.
(
string
)
}
else
{
traceId
=
strings
.
Replace
(
uuid
.
NewV4
()
.
String
(),
"-"
,
""
,
-
1
)
}
var
userId
string
if
userIdTemp
,
ok
:=
ctx
[
USERID
];
ok
{
userId
=
strconv
.
FormatUint
(
userIdTemp
.
(
uint64
),
10
)
}
var
deviceTypeStr
string
if
deviceTypeTemp
,
ok
:=
ctx
[
DEVICETYPE
];
ok
{
deviceTypeStr
,
ok
=
deviceTypeTemp
.
(
string
)
}
var
sAppVersion
string
if
appVersionTmp
,
ok
:=
ctx
[
APP_VERSION
];
ok
{
sAppVersion
,
ok
=
appVersionTmp
.
(
string
)
}
var
url
string
if
urlTmp
,
ok
:=
ctx
[
URL
];
ok
{
url
,
ok
=
urlTmp
.
(
string
)
}
var
method
string
if
methodTmp
,
ok
:=
ctx
[
METHOD
];
ok
{
method
,
ok
=
methodTmp
.
(
string
)
}
_ctx
:=
context
.
WithValue
(
context
.
Background
(),
"traceId"
,
traceId
)
_ctx
=
context
.
WithValue
(
_ctx
,
"userId"
,
userId
)
return
&
MyContext
{
Context
:
_ctx
,
Log
:
CreateContextLog
(
userId
,
traceId
,
deviceTypeStr
,
sAppVersion
,
url
,
method
),
Cxt
:
ctx
,
}
}
/**
* 创建上下文的日志
**/
func
CreateContextLog
(
userId
string
,
traceId
string
,
deviceType
string
,
deviceVersion
string
,
url
string
,
method
string
)
*
logrus
.
Entry
{
return
mylogrus
.
MyLog
.
WithFields
(
logrus
.
Fields
{
USERID
:
userId
,
TRACEID
:
traceId
,
DEVICETYPE
:
deviceType
,
APP_VERSION
:
deviceVersion
,
URL
:
url
,
METHOD
:
method
,
})
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment