Commit 941337a6 authored by Tsaiilin's avatar Tsaiilin

新增 env 控制程序行为

parent 630f8262
......@@ -18,6 +18,7 @@ import (
"virjar.com/majora-go/client"
"virjar.com/majora-go/common"
"virjar.com/majora-go/env"
"virjar.com/majora-go/log"
"virjar.com/majora-go/model"
"virjar.com/majora-go/safe"
......@@ -58,6 +59,9 @@ func init() {
}
func initial(cfg *model.Configure) {
// 忽略错误,默认 product 环境
_ = env.CurrentEnv.Set(cfg.Env)
log.Init(cfg.LogLevel)
if cfg.PprofPort > 0 {
safe.SageGo(func() {
......
env = debug
tunnel_addr = 127.0.0.1:5879
;tunnel_addr = aoba.vip:5879
dns_server = 114.114.114.114:53
......
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
}
......@@ -8,11 +8,11 @@ import (
"path/filepath"
"github.com/adamweixuan/getty"
"virjar.com/majora-go/env"
)
var (
runLogger getty.Logger
eventLogger getty.Logger
traceLogger *zap.Logger
errorLogger getty.Logger
)
......@@ -25,7 +25,7 @@ const (
)
// debug 模式下会将日志输出到控制台和文件,其他模式只输出到文件
func getLogWriter(path string, level zapcore.Level) zapcore.WriteSyncer {
func getLogWriter(path string) zapcore.WriteSyncer {
lumberJackLogger := &lumberjack.Logger{
Filename: path,
MaxSize: 10,
......@@ -33,7 +33,7 @@ func getLogWriter(path string, level zapcore.Level) zapcore.WriteSyncer {
MaxAge: 30,
Compress: false,
}
if level == zapcore.DebugLevel {
if env.CurrentEnv == env.Debug {
return zapcore.NewMultiWriteSyncer(zapcore.AddSync(os.Stdout), zapcore.AddSync(lumberJackLogger))
}
return zapcore.AddSync(lumberJackLogger)
......@@ -84,7 +84,7 @@ func initLogger(path string, caller bool, level string) *zap.Logger {
_ = zapLevel.Set(level)
encoder := getEncoder()
writeSyncer := getLogWriter(path, zapLevel)
writeSyncer := getLogWriter(path)
core := zapcore.NewCore(encoder, writeSyncer, zapLevel)
if caller {
return zap.New(core, zap.AddCaller())
......
......@@ -20,6 +20,7 @@ type Extra struct {
}
type Configure struct {
Env string `ini:"env" json:"env"`
LogLevel string `ini:"log_level" json:"log_level"`
PprofPort int `ini:"pprof_port" json:"pprof_port"`
TunnelAddr string `ini:"tunnel_addr" json:"tunnel_addr"`
......@@ -39,6 +40,7 @@ const (
func NewDefMajoraConf() *Configure {
return &Configure{
Env: "product",
LogLevel: "info",
PprofPort: 0,
TunnelAddr: common.DefNatAddr,
......
......@@ -3,12 +3,13 @@ package trace
import (
"sync/atomic"
"time"
"virjar.com/majora-go/safe"
"github.com/adamweixuan/getty"
"go.uber.org/zap"
"virjar.com/majora-go/env"
"virjar.com/majora-go/log"
"virjar.com/majora-go/safe"
)
var (
......@@ -82,7 +83,6 @@ type recorderImpl struct {
sessionId string
}
func (r *recorderImpl) RecordEvent(eventName string, message string) {
r.RecordErrorEvent(eventName, message, nil)
}
......@@ -118,6 +118,9 @@ var defaultNopRecorder = nopRecorder{}
var slots = make([]int64, 30)
func acquireRecorder(sessionId string) Recorder {
if env.CurrentEnv == env.Debug {
return &recorderImpl{sessionId: sessionId}
}
now := time.Now()
slotIndex := now.Minute() / 2
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment