react-native实现本地记录日志,对java不是很熟悉,用go写的记录日志,react-native调用
首先go程序androidLog.go
package android_log
import (
"fmt"
"os"
"log"
"io"
"sync"
"runtime"
. "rc_util"
)
const (
ANDROID_LOG_ERROR = iota
ANDROID_LOG_WARN
ANDROID_LOG_INFO
ANDROID_LOG_XORM
ANDROID_LOG_DEBUG
)
const (
ANDROIDCALLDEPTH = 2
)
var logger *log.Logger
var loglevel int = ANDROID_LOG_DEBUG
var logfile *os.File
var strfile string
var logMutex sync.Mutex
func InitAndroidLog(rc_path string,log_file_name string, ll int) {
loglevel = ll
/* curr_path, _ := os.Getwd()
logPath := curr_path + "/logs"
if !IsExist(logPath) {
os.MkdirAll(logPath, os.ModePerm)
}*/
if rc_path[len(rc_path)-1:] != "/" {
rc_path += "/"
}
//rc_path += "rc/"
if !IsExist(rc_path) {
os.MkdirAll(rc_path, os.ModePerm)
}
var err error
strfile = rc_path + log_file_name
logfile, err = os.OpenFile(strfile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
//logfile, err = os.OpenFile(strfile, os.O_CREATE | os.O_APPEND, os.ModePerm)
if err != nil {
fmt.Printf("%s\r\n", err.Error())
os.Exit(-1)
}
logger = log.New(logfile, "",2)
}
func ReleaseAndroidLog() {
if logfile != nil {
logfile.Close()
logfile = nil
}
}
func CopyAndroidFile(src, des string) (w int64, err error) {
srcFile, err := os.Open(src)
if err != nil {
fmt.Println(err)
}
defer srcFile.Close()
desFile, err := os.Create(des)
if err != nil {
fmt.Println(err)
}
defer desFile.Close()
return io.Copy(desFile, srcFile)
}
func checkFileSize() {
fs, err := os.Stat(strfile)
if err == nil {
if fs.Size() > 1024*1024*10 {
ReleaseLog()
w, err := CopyAndroidFile(strfile + "~~", strfile + "~~~")
w, err = CopyAndroidFile(strfile + "~", strfile + "~~")
w, err = CopyAndroidFile(strfile, strfile + "~")
if err != nil {
fmt.Println(err.Error())
} else {
fmt.Println(w)
os.Remove(strfile)
//logfile, err = os.OpenFile(strfile, os.O_CREATE | os.O_APPEND, os.ModePerm)
logfile, err = os.OpenFile(strfile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
fmt.Printf("%s\r\n", err.Error())
os.Exit(-1)
}
//logger = log.New(logfile, "", log.Ldate | log.Ltime | log.Lshortfile)
logger = log.New(logfile, "", log.Ldate | log.Ltime)
}
}
}
}
func AndroidGetFileInfo(skip int) string {
fun_name := "???"
pc, file, line, ok := runtime.Caller(skip)
if !ok {
file = "???"
line = 0
} else {
short := file
for i := len(file) - 1; i > 0; i-- {
if file[i] == '/' {
short = file[i+1:]
break
}
}
file = short
fun_name = runtime.FuncForPC(pc).Name()
}
return fmt.Sprint(" ", file, ".", line, "[", fun_name, "] ")
}
func LogAndroidXorm(message ...interface{}) {
logMutex.Lock()
defer logMutex.Unlock()
if loglevel >= ANDROID_LOG_XORM {
logger.Print(message...);
}
checkFileSize()
}
func LogAndroidDebug(message ...interface{}) {
logMutex.Lock()
defer logMutex.Unlock()
if loglevel >= ANDROID_LOG_DEBUG {
value := fmt.Sprint(GetNowTime().In(GetLocal()).Format("2006-01-02 15:04:05"),"[DBG]", AndroidGetFileInfo(ANDROIDCALLDEPTH), fmt.Sprint(message...))
fmt.Println(value)
logger.Println(value);
}
checkFileSize()
}
func LogAndroidInfo(message ...interface{}) {
logMutex.Lock()
defer logMutex.Unlock()
if loglevel >= ANDROID_LOG_INFO {
value := fmt.Sprint("[INF]", AndroidGetFileInfo(ANDROIDCALLDEPTH), fmt.Sprint(message...))
fmt.Println(value)
logger.Println(value);
}
checkFileSize()
}
func LogAndroidWarn(message ...interface{}) {
logMutex.Lock()
defer logMutex.Unlock()
if loglevel >= ANDROID_LOG_WARN {
value := fmt.Sprint("[WRN]", AndroidGetFileInfo(ANDROIDCALLDEPTH), fmt.Sprint(message...))
fmt.Println(value)
logger.Println(value);
}
checkFileSize()
}
func LogAndroidError(message ...interface{}) {
logMutex.Lock()
defer logMutex.Unlock()
if loglevel >= ANDROID_LOG_ERROR {
value := fmt.Sprint("[ERR]", AndroidGetFileInfo(ANDROIDCALLDEPTH), fmt.Sprint(message...))
fmt.Println(value)
logger.Println(value);
}
checkFileSize()
}
func LogAndroidError3(message ...interface{}) {
logMutex.Lock()
defer logMutex.Unlock()
if loglevel >= ANDROID_LOG_ERROR {
value := fmt.Sprint("[ERR]", AndroidGetFileInfo(3), fmt.Sprint(message...))
fmt.Println(value)
logger.Println(value);
}
checkFileSize()
}
androidLog.go
文件路径如下
package androidLog
import (
. "rc_util"
."androidLog/rc_http/controller/android_log"
)
var filterPrefixes = make([]string, 0, 5)
var ROOT = GetRootPath()
const (
PACKAGE_NAME = "android_log"
)
func GenerateLog(word string){
LogAndroidDebug(word)
}
func StartLog(rc_path string){
InitAndroidLog(rc_path,"android_log", ANDROID_LOG_DEBUG)
}
func Release(){
ReleaseAndroidLog()
}
修改GOROOT下面的src/golang.org/x/mobile/example/bind/android/hello/build.gradle,配置GOPATH,GO和GOMOBILE的路径
文件名和入口的go文件名称相同
参数:arm(设备) 386(虚拟机,模拟器)
在go程序的根目录下运行:
gomobile bind -v -target=android/arm
之后会生成文件,放在android目录下,在android文件配置调用即可
android文件配置调用
引入
import androidLog.AndroidLog;
@ReactMethod
public void initLog() {
Log.i("初始化日志获取路径",getPaths());
androidLog.AndroidLog.startLog("/sdcard/Handheld/");
}
react-native调用
NativeModules.Go.setLog("日志数据");