Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
M
majora-go
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
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
majora
majora-go
Commits
941337a6
Commit
941337a6
authored
Dec 23, 2021
by
Tsaiilin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
新增 env 控制程序行为
parent
630f8262
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
61 additions
and
6 deletions
+61
-6
cli.go
cli.go
+4
-0
majora-dev.ini
conf/majora-dev.ini
+1
-0
env.go
env/env.go
+45
-0
logger.go
log/logger.go
+4
-4
config.go
model/config.go
+2
-0
trace.go
trace/trace.go
+5
-2
No files found.
cli.go
View file @
941337a6
...
@@ -18,6 +18,7 @@ import (
...
@@ -18,6 +18,7 @@ import (
"virjar.com/majora-go/client"
"virjar.com/majora-go/client"
"virjar.com/majora-go/common"
"virjar.com/majora-go/common"
"virjar.com/majora-go/env"
"virjar.com/majora-go/log"
"virjar.com/majora-go/log"
"virjar.com/majora-go/model"
"virjar.com/majora-go/model"
"virjar.com/majora-go/safe"
"virjar.com/majora-go/safe"
...
@@ -58,6 +59,9 @@ func init() {
...
@@ -58,6 +59,9 @@ func init() {
}
}
func
initial
(
cfg
*
model
.
Configure
)
{
func
initial
(
cfg
*
model
.
Configure
)
{
// 忽略错误,默认 product 环境
_
=
env
.
CurrentEnv
.
Set
(
cfg
.
Env
)
log
.
Init
(
cfg
.
LogLevel
)
log
.
Init
(
cfg
.
LogLevel
)
if
cfg
.
PprofPort
>
0
{
if
cfg
.
PprofPort
>
0
{
safe
.
SageGo
(
func
()
{
safe
.
SageGo
(
func
()
{
...
...
conf/majora-dev.ini
View file @
941337a6
env
=
debug
tunnel_addr
=
127.0.0.1:5879
tunnel_addr
=
127.0.0.1:5879
;tunnel_addr = aoba.vip:5879
;tunnel_addr = aoba.vip:5879
dns_server
=
114.114.114.114:53
dns_server
=
114.114.114.114:53
...
...
env/env.go
0 → 100644
View file @
941337a6
package
env
import
(
"bytes"
"errors"
"fmt"
)
var
CurrentEnv
=
Product
var
errUnmarshalEnv
=
errors
.
New
(
"can't unmarshal a nil *Level"
)
type
Env
int8
const
(
Debug
Env
=
iota
-
1
Product
)
func
(
e
*
Env
)
Set
(
s
string
)
error
{
return
e
.
UnmarshalText
([]
byte
(
s
))
}
func
(
e
*
Env
)
UnmarshalText
(
text
[]
byte
)
error
{
if
e
==
nil
{
return
errUnmarshalEnv
}
if
!
e
.
unmarshalText
(
text
)
&&
!
e
.
unmarshalText
(
bytes
.
ToLower
(
text
))
{
return
fmt
.
Errorf
(
"unrecognized env: %q"
,
text
)
}
return
nil
}
func
(
e
*
Env
)
unmarshalText
(
text
[]
byte
)
bool
{
switch
string
(
text
)
{
case
"debug"
,
"DEBUG"
:
*
e
=
Debug
case
"product"
,
"PRODUCT"
,
""
:
// make the zero value useful
*
e
=
Product
default
:
return
false
}
return
true
}
log/logger.go
View file @
941337a6
...
@@ -8,11 +8,11 @@ import (
...
@@ -8,11 +8,11 @@ import (
"path/filepath"
"path/filepath"
"github.com/adamweixuan/getty"
"github.com/adamweixuan/getty"
"virjar.com/majora-go/env"
)
)
var
(
var
(
runLogger
getty
.
Logger
runLogger
getty
.
Logger
eventLogger
getty
.
Logger
traceLogger
*
zap
.
Logger
traceLogger
*
zap
.
Logger
errorLogger
getty
.
Logger
errorLogger
getty
.
Logger
)
)
...
@@ -25,7 +25,7 @@ const (
...
@@ -25,7 +25,7 @@ const (
)
)
// debug 模式下会将日志输出到控制台和文件,其他模式只输出到文件
// debug 模式下会将日志输出到控制台和文件,其他模式只输出到文件
func
getLogWriter
(
path
string
,
level
zapcore
.
Level
)
zapcore
.
WriteSyncer
{
func
getLogWriter
(
path
string
)
zapcore
.
WriteSyncer
{
lumberJackLogger
:=
&
lumberjack
.
Logger
{
lumberJackLogger
:=
&
lumberjack
.
Logger
{
Filename
:
path
,
Filename
:
path
,
MaxSize
:
10
,
MaxSize
:
10
,
...
@@ -33,7 +33,7 @@ func getLogWriter(path string, level zapcore.Level) zapcore.WriteSyncer {
...
@@ -33,7 +33,7 @@ func getLogWriter(path string, level zapcore.Level) zapcore.WriteSyncer {
MaxAge
:
30
,
MaxAge
:
30
,
Compress
:
false
,
Compress
:
false
,
}
}
if
level
==
zapcore
.
DebugLevel
{
if
env
.
CurrentEnv
==
env
.
Debug
{
return
zapcore
.
NewMultiWriteSyncer
(
zapcore
.
AddSync
(
os
.
Stdout
),
zapcore
.
AddSync
(
lumberJackLogger
))
return
zapcore
.
NewMultiWriteSyncer
(
zapcore
.
AddSync
(
os
.
Stdout
),
zapcore
.
AddSync
(
lumberJackLogger
))
}
}
return
zapcore
.
AddSync
(
lumberJackLogger
)
return
zapcore
.
AddSync
(
lumberJackLogger
)
...
@@ -84,7 +84,7 @@ func initLogger(path string, caller bool, level string) *zap.Logger {
...
@@ -84,7 +84,7 @@ func initLogger(path string, caller bool, level string) *zap.Logger {
_
=
zapLevel
.
Set
(
level
)
_
=
zapLevel
.
Set
(
level
)
encoder
:=
getEncoder
()
encoder
:=
getEncoder
()
writeSyncer
:=
getLogWriter
(
path
,
zapLevel
)
writeSyncer
:=
getLogWriter
(
path
)
core
:=
zapcore
.
NewCore
(
encoder
,
writeSyncer
,
zapLevel
)
core
:=
zapcore
.
NewCore
(
encoder
,
writeSyncer
,
zapLevel
)
if
caller
{
if
caller
{
return
zap
.
New
(
core
,
zap
.
AddCaller
())
return
zap
.
New
(
core
,
zap
.
AddCaller
())
...
...
model/config.go
View file @
941337a6
...
@@ -20,6 +20,7 @@ type Extra struct {
...
@@ -20,6 +20,7 @@ type Extra struct {
}
}
type
Configure
struct
{
type
Configure
struct
{
Env
string
`ini:"env" json:"env"`
LogLevel
string
`ini:"log_level" json:"log_level"`
LogLevel
string
`ini:"log_level" json:"log_level"`
PprofPort
int
`ini:"pprof_port" json:"pprof_port"`
PprofPort
int
`ini:"pprof_port" json:"pprof_port"`
TunnelAddr
string
`ini:"tunnel_addr" json:"tunnel_addr"`
TunnelAddr
string
`ini:"tunnel_addr" json:"tunnel_addr"`
...
@@ -39,6 +40,7 @@ const (
...
@@ -39,6 +40,7 @@ const (
func
NewDefMajoraConf
()
*
Configure
{
func
NewDefMajoraConf
()
*
Configure
{
return
&
Configure
{
return
&
Configure
{
Env
:
"product"
,
LogLevel
:
"info"
,
LogLevel
:
"info"
,
PprofPort
:
0
,
PprofPort
:
0
,
TunnelAddr
:
common
.
DefNatAddr
,
TunnelAddr
:
common
.
DefNatAddr
,
...
...
trace/trace.go
View file @
941337a6
...
@@ -3,12 +3,13 @@ package trace
...
@@ -3,12 +3,13 @@ package trace
import
(
import
(
"sync/atomic"
"sync/atomic"
"time"
"time"
"virjar.com/majora-go/safe"
"github.com/adamweixuan/getty"
"github.com/adamweixuan/getty"
"go.uber.org/zap"
"go.uber.org/zap"
"virjar.com/majora-go/env"
"virjar.com/majora-go/log"
"virjar.com/majora-go/log"
"virjar.com/majora-go/safe"
)
)
var
(
var
(
...
@@ -82,7 +83,6 @@ type recorderImpl struct {
...
@@ -82,7 +83,6 @@ type recorderImpl struct {
sessionId
string
sessionId
string
}
}
func
(
r
*
recorderImpl
)
RecordEvent
(
eventName
string
,
message
string
)
{
func
(
r
*
recorderImpl
)
RecordEvent
(
eventName
string
,
message
string
)
{
r
.
RecordErrorEvent
(
eventName
,
message
,
nil
)
r
.
RecordErrorEvent
(
eventName
,
message
,
nil
)
}
}
...
@@ -118,6 +118,9 @@ var defaultNopRecorder = nopRecorder{}
...
@@ -118,6 +118,9 @@ var defaultNopRecorder = nopRecorder{}
var
slots
=
make
([]
int64
,
30
)
var
slots
=
make
([]
int64
,
30
)
func
acquireRecorder
(
sessionId
string
)
Recorder
{
func
acquireRecorder
(
sessionId
string
)
Recorder
{
if
env
.
CurrentEnv
==
env
.
Debug
{
return
&
recorderImpl
{
sessionId
:
sessionId
}
}
now
:=
time
.
Now
()
now
:=
time
.
Now
()
slotIndex
:=
now
.
Minute
()
/
2
slotIndex
:=
now
.
Minute
()
/
2
...
...
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