Skip to content

Commit

Permalink
Merge pull request #1352 from MyRobotLab/log-config
Browse files Browse the repository at this point in the history
config added to Log service
  • Loading branch information
kwatters authored Oct 12, 2023
2 parents a94914a + fd3c1ff commit c9dd10f
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 16 deletions.
45 changes: 33 additions & 12 deletions src/main/java/org/myrobotlab/service/Log.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
import org.myrobotlab.logging.LoggerFactory;
import org.myrobotlab.logging.Logging;
import org.myrobotlab.logging.LoggingFactory;
import org.myrobotlab.service.config.ServiceConfig;
import org.myrobotlab.service.config.LogConfig;
import org.myrobotlab.service.config.RuntimeConfig;
import org.slf4j.Logger;

import ch.qos.logback.classic.spi.ILoggingEvent;
Expand All @@ -44,7 +45,7 @@
import ch.qos.logback.core.spi.FilterReply;
import ch.qos.logback.core.status.Status;

public class Log extends Service<ServiceConfig> implements Appender<ILoggingEvent> {
public class Log extends Service<LogConfig> implements Appender<ILoggingEvent> {

public static class LogEntry {
public long ts;
Expand Down Expand Up @@ -81,7 +82,7 @@ public String toString() {
* broadcast logging is through publishLogEvent (not broadcastState)
*/
transient List<LogEntry> buffer = new ArrayList<>();

/**
* logging state
*/
Expand All @@ -92,11 +93,8 @@ public String toString() {
*/
long lastPublishLogTimeTs = 0;

/**
* current log level
*/
String logLevel = null;


/**
* max size of log buffer
*/
Expand All @@ -114,8 +112,10 @@ public Log(String n, String id) {

public String getLogLevel() {
Logging logging = LoggingFactory.getInstance();
logLevel = logging.getLevel();
return logLevel;
if (config != null) {
config.level = logging.getLevel();
}
return logging.getLevel();
}

@Override
Expand Down Expand Up @@ -192,6 +192,7 @@ public void doAppend(ILoggingEvent event) throws LogbackException {
synchronized public void flush() {
if (buffer.size() > 0) {
invoke("publishLogEvents", buffer);

buffer = new ArrayList<>(maxSize);
lastPublishLogTimeTs = System.currentTimeMillis();
}
Expand Down Expand Up @@ -224,6 +225,11 @@ public List<LogEntry> publishLogEvents(List<LogEntry> entries) {
return entries;
}

public List<LogEntry> publishErrors(List<LogEntry> entries) {
return entries;
}


@Override
public void setContext(Context arg0) {
// TODO Auto-generated method stub
Expand Down Expand Up @@ -256,6 +262,18 @@ public void setRootLogLevel(String level) {
getLogLevel();
broadcastState();
}

public LogConfig apply(LogConfig c) {
super.apply(c);
if (c.level != null) {
setRootLogLevel(c.level);
}
return c;
}

public LogConfig getConfig() {
return config;
}

@Override
public void setName(String name) {
Expand Down Expand Up @@ -308,14 +326,17 @@ public static void main(String[] args) {
try {

// Log4jLoggerAdapter blah;

Runtime runtime = Runtime.getInstance();
RuntimeConfig config = runtime.getConfig();
config.resource = "src/main/resources/resource";
runtime.apply(config);

Runtime.start("log", "Log");
Runtime.start("python", "Python");
WebGui webgui = (WebGui) Runtime.create("webgui", "WebGui");
webgui.autoStartBrowser(false);
webgui.startService();
Runtime runtime = Runtime.getInstance();
runtime.startInteractiveMode();

log.info("this is an info test");
log.warn("this is an warn test");
log.error("this is an error test");
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/org/myrobotlab/service/config/LogConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.myrobotlab.service.config;

public class LogConfig extends ServiceConfig {

/**
* level of log error, warn, info, debug
*/
public String level = "info";

}
6 changes: 3 additions & 3 deletions src/main/resources/resource/WebGui/app/service/js/LogGui.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
angular.module('mrlapp.service.LogGui', []).controller('LogGuiCtrl', ['$scope', '$log', 'mrl', function($scope, $log, mrl) {
$log.info('LogGuiCtrl')
angular.module('mrlapp.service.LogGui', []).controller('LogGuiCtrl', ['$scope', 'mrl', function($scope, mrl) {
console.info('LogGuiCtrl')
var _self = this
var msg = this.msg

Expand Down Expand Up @@ -116,7 +116,7 @@ angular.module('mrlapp.service.LogGui', []).controller('LogGuiCtrl', ['$scope',
}
break
default:
$log.error("ERROR - unhandled method " + $scope.name + "." + msg.method)
console.error("ERROR - unhandled method " + $scope.name + "." + msg.method)
break
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<button class="btn btn-default" ng-show="!service.isLogging" ng-click="msg.startLogging();msg.broadcastState()">start logging</button>
<button class="btn btn-default" ng-show="service.isLogging" ng-click="msg.stopLogging();msg.broadcastState()">stop logging</button>
<div class="btn-group" uib-dropdown>
<button id="level-button" type="button" ng-class="{'btn': true, 'btn-success': service.logLevel == 'DEBUG','btn-default': service.logLevel == 'INFO', 'btn-warning': service.logLevel == 'WARN', 'btn-danger': service.logLevel == 'ERROR','small': true}" uib-dropdown-toggle>log level - {{service.logLevel}}</button>
<button id="level-button" type="button" ng-class="{'btn': true, 'btn-success': service.logLevel == 'DEBUG','btn-default': service.logLevel == 'INFO', 'btn-warning': service.logLevel == 'WARN', 'btn-danger': service.logLevel == 'ERROR','small': true}" uib-dropdown-toggle>log level - {{service.config.level}}</button>
<ul uib-dropdown-menu role="menu">
<li>
<button class="btn btn-success col-md-12" ng-click="msg.setRootLogLevel('debug')">DEBUG</button>
Expand Down

0 comments on commit c9dd10f

Please sign in to comment.