Skip to content

Commit

Permalink
log format json
Browse files Browse the repository at this point in the history
  • Loading branch information
gerassimos committed Nov 3, 2022
1 parent e3b7e60 commit 32a956b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
13 changes: 12 additions & 1 deletion resources/iot-c/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,15 @@ grpcurl --plaintext -d '{"name": "test222"}' localhost:9091 com.gmos.iotc.proto.
- Use the `/gnmi` rest end point in the collector-ui service to test grpc
- New env variables defined for the gnmi device are:
- `GNMI_GRPC_HOST_NAME` -> the ip address of the grpc/gnmi network device (target)
- `GNMI_GRPC_PORT` -> the grpc port of the grpc/gnmi network device (target)
- `GNMI_GRPC_PORT` -> the grpc port of the grpc/gnmi network device (target)

## log format json
- To enable log format add the following to the application.yml
- This is a quick a "dirty" way to have json log, probably we need to use more clean approach
- [ref](https://stackoverflow.com/questions/53730449/is-there-a-recommended-way-to-get-spring-boot-to-json-format-logs-with-logback)
```yaml
logging:
#https://stackoverflow.com/questions/53730449/is-there-a-recommended-way-to-get-spring-boot-to-json-format-logs-with-logback
pattern:
console: "{\"time\": \"%d\", \"level\": \"%p\", \"correlation-id\": \"%X{X-Correlation-Id}\", \"source\": \"%logger{63}:%L\", \"message\": \"%replace(%m%wEx{6}){'[\r\n]+', '\\n'}%nopex\"}%n"
```
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;

@RestController
Expand All @@ -27,6 +29,19 @@ public DataRestController(DataHdrl dataHdrl, DataHdrlGrpcClient dataHdrlGrpcClie
this.gnmiHdrlGrpcClient = gnmiHdrlGrpcClient;
}

@GetMapping("/hello")
public String hello() {
String host = "UnknownHost";
try{
host = InetAddress.getLocalHost().getHostName();
}
catch (UnknownHostException e ){
logger.error("Failed to get hostname: {}", e.getMessage());
}
logger.info("Hello from {}", host);
return "Hello from " + host;
}

@GetMapping("/getData")
public List<PerformanceDataDTO> getData() {
logger.debug("Get Request getData");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ spring:
logging:
level:
com.gmos.iotc: ${LOG_LEVEL:DEBUG}
#https://stackoverflow.com/questions/53730449/is-there-a-recommended-way-to-get-spring-boot-to-json-format-logs-with-logback
pattern:
console: "{\"time\": \"%d\", \"level\": \"%p\", \"correlation-id\": \"%X{X-Correlation-Id}\", \"source\": \"%logger{63}:%L\", \"message\": \"%replace(%m%wEx{6}){'[\r\n]+', '\\n'}%nopex\"}%n"

server:
port: ${TOMCAT_HTTP_PORT_V:8093}
Expand Down

0 comments on commit 32a956b

Please sign in to comment.