Skip to content

Commit

Permalink
feat(android): Better format log
Browse files Browse the repository at this point in the history
  • Loading branch information
gfanton committed Oct 18, 2018
1 parent d2d6668 commit 4782a2e
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 30 deletions.
18 changes: 18 additions & 0 deletions client/react-native/Makefile
Expand Up @@ -53,6 +53,11 @@ deps.ios: deps
.PHONY: deps.web
deps.web: deps
cd $(WEB) && yarn

.PHONY: deps.logs
deps.logs:
brew install --HEAD pidcat

##############################


Expand Down Expand Up @@ -82,6 +87,19 @@ debug.web: deps.web
##############################


### Logs part ###
.PHONY: log.android
log.android:
@(command -v pidcat > /dev/null && pidcat --always-display-tags -t chat.berty.*) || \
(echo "Consider to install pidcat (brew install pidcat) for a better logging experience" && sleep 2 && adb logcat)

.PHONY: log.ios
log.ios:
@open '/Applications/Utilities/Console.app'

##############################


### Clean dependencies part ###
.PHONY: clean.bundler_and_pkgs
clean.bundler_and_pkgs:
Expand Down
Expand Up @@ -8,7 +8,7 @@
import core.Core;

public class CoreModule extends ReactContextBaseJavaModule {

private Logger logger = new Logger("chat.berty.io");
private String filesDir = "";
private ReactApplicationContext reactContext;

Expand All @@ -23,11 +23,12 @@ public String getName() {
}

@ReactMethod
public void start(Promise promise) {
public void start(Promise promise) throws Exception {
try {
Core.start(this.filesDir);
Core.start(this.filesDir, this.logger);
promise.resolve(null);
} catch (Exception err) {
this.logger.format(Level.ERROR, this.getName(), "Unable to start core: %s", err);
promise.reject(err);
}
}
Expand All @@ -38,6 +39,7 @@ public void getPort(Promise promise) {
Long port = Core.getPort();
promise.resolve(port.toString());
} catch (Exception err) {
this.logger.format(Level.ERROR, this.getName(), "Unable to get port", err);
promise.reject(err);
}
}
Expand Down
@@ -0,0 +1,24 @@
package chat.berty.core;

public enum Level
{
DEBUG("DEBUG"),
INFO("INFO"),
WARN("WARN"),
ERROR("ERROR"),
PANIC("PANIC"),
DPANIC("DPANIC"),
FATAL("FATAL"),
UNKNOW("UNKNOW");

private final String level;

Level(final String level) {
this.level = level;
}

@Override
public String toString() {
return level;
}
}
@@ -0,0 +1,67 @@
package chat.berty.core;

import android.util.Log;

public class Logger implements core.Logger {
boolean isEnabled = true;
String subsytem = "logger";

public Logger(String subsytem) {
this.subsytem = subsytem;
}

// @TODO: implement this
@Override
public boolean levelEnabler(String level) {
return this.isEnabled;
}

public void format(Level level, String namespace, String format, Object... args) {
final String message = String.format(format, args);
try {
this.log(level.toString(), namespace, message);
} catch (Exception e) {
final String tag = this.subsytem + "." + namespace;
Log.e(tag, "Unable to use logger: " + e.getMessage());
Log.e(tag, message);
}
}

@Override
public void log(String level, String namespace, String message) throws Exception {
final Level currentLevel = Level.valueOf(level);
final String tag = this.subsytem + "." + namespace;

switch(currentLevel) {
case DEBUG:
Log.d(tag, message);
break;
case INFO:
Log.i(tag, message);
break;
case WARN:
Log.w(tag, message);
break;
case ERROR:
Log.e(tag, message);
break;
case PANIC:
Log.wtf(tag, message);
break;
case DPANIC:
Log.wtf(tag, message);
break;
case FATAL:
Log.wtf(tag, message);
break;
case UNKNOW:
Log.i(tag, message);
break;
default:
Log.i(tag, message);
break;


}
}
}
1 change: 0 additions & 1 deletion client/react-native/gomobile/Makefile
Expand Up @@ -39,7 +39,6 @@ deps:
patch -N ~/go/src/golang.org/x/mobile/cmd/gomobile/env.go $(BUILD_PATCH_PATH)/gomobile-env-flag.diff || true
go install golang.org/x/mobile/cmd/gomobile
go get -v golang.org/x/mobile/cmd/gobind
gomobile init

.PHONY: init
init: deps
Expand Down
12 changes: 2 additions & 10 deletions client/react-native/gomobile/core.go
Expand Up @@ -82,19 +82,11 @@ func Start(datastorePath string, logger Logger) error {
return nil
}

// initialize logger
if err := setupLogger("debug", logger); err != nil {
return err
}

// // initialize logger
// cfg := zap.NewDevelopmentConfig()
// cfg.Level.SetLevel(zap.DebugLevel)
// l, err := cfg.Build()
// if err != nil {
// panic(err)
// }
// zap.ReplaceGlobals(l)

if err := setGqlPort(); err != nil {
return err
}
Expand Down Expand Up @@ -185,7 +177,7 @@ func daemon(opts *daemonOptions) error {
}

if addr.IP == nil {
addr.IP = net.IP{127, 0, 0, 1}
addr.IP = net.IP{0, 0, 0, 0}
}

listener, err := reuse.Listen(addr.Network(), fmt.Sprintf("%s:%d", addr.IP.String(), addr.Port))
Expand Down
30 changes: 14 additions & 16 deletions client/react-native/gomobile/logger.go
Expand Up @@ -33,21 +33,12 @@ func (mc *mobileCore) Check(entry zapcore.Entry, checked *zapcore.CheckedEntry)
}

func (mc *mobileCore) Write(entry zapcore.Entry, fields []zapcore.Field) error {
var out string

out = entry.Message
if len(fields) > 0 {
out += ": { "
for i, f := range fields {
out += f.Key + ": " + f.String
if i < len(fields)-1 {
out += ", "
}
}
out += " }"
buff, err := mc.enc.EncodeEntry(entry, fields)
if err != nil {
return err
}

return mc.l.Log(entry.Level.CapitalString(), entry.LoggerName, out)
return mc.l.Log(entry.Level.CapitalString(), entry.LoggerName, buff.String())
}

type p2pLogBackendWrapper struct {
Expand Down Expand Up @@ -107,18 +98,25 @@ func setupLogger(logLevel string, mlogger Logger) error {
config := zap.NewDevelopmentConfig()
config.Level.SetLevel(zapLogLevel)
config.DisableStacktrace = true
config.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder

// configure log encoder
// disable unwanted info
config.EncoderConfig.LevelKey = ""
config.EncoderConfig.TimeKey = ""
config.EncoderConfig.NameKey = ""
config.EncoderConfig.CallerKey = ""

l, err := config.Build()
if err != nil {
return err
}

consoleEncoder := zapcore.NewConsoleEncoder(config.EncoderConfig)
mobile := zap.WrapCore(func(core zapcore.Core) zapcore.Core {
mobileCore := zap.WrapCore(func(core zapcore.Core) zapcore.Core {
return newMobileCore(core, consoleEncoder, mlogger)
})

zap.ReplaceGlobals(l.WithOptions(mobile))
zap.ReplaceGlobals(l.WithOptions(mobileCore))
logger().Debug("logger initialized")

// configure p2p log
Expand Down

0 comments on commit 4782a2e

Please sign in to comment.