Skip to content

Commit 167a22a

Browse files
committed
Feat: add event setter
1 parent 7791c63 commit 167a22a

File tree

9 files changed

+52
-28
lines changed

9 files changed

+52
-28
lines changed

README.md

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,10 @@ data := map[string]interface{}{
7676
7777
json, _ := json.Marshal(data)
7878
79-
logger.Error("Hello World", rootle.String(string(json)), &rootle.Downstream{
79+
// can be chained in the New function.
80+
logger.WithEvent(string(json))
81+
82+
logger.Error("Hello World", &rootle.Downstream{
8083
Http: &rootle.Http{
8184
Method: "GET",
8285
StatusCode: rootle.INTERNAL_SERVER_ERROR,
@@ -133,18 +136,18 @@ val logger = Rootle("ac12Cd-Aevd-12Grx-235f4", "Billing-lambda")
133136
logger.info("Hello World")
134137
logger.warn("Hello World")
135138
139+
val jsonObject = JsonObject()
140+
jsonObject.addProperty("foo", "bar")
141+
// Can be set in Rootle initlization `Rootle(id, application, event)`
142+
logger.setEvent(jsonObject.toString())
136143
137-
val logger = Rootle("ac12Cd-Aevd-12Grx-235f4", "billing")
138-
139-
val jsonObject = JsonObject()
140-
jsonObject.addProperty("foo", "bar")
144+
logger.error("Error message", rootle.Downstream(rootle.Http("GET", StatusCode.InternalServerError.code, "http://localhost:8080/invoice/123",
145+
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36",
146+
"http://localhost:8080/", jsonObject.toString()),
147+
rootle.Grpc("GetInvoice", GrpcCodes.internalError.code, "invoice",
148+
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36",
149+
"http://localhost:8080/", jsonObject.toString())), "billing/user", 0);
141150
142-
logger.error("Error message", jsonObject.toString(), rootle.Downstream(rootle.Http("GET", StatusCode.InternalServerError.code, "http://localhost:8080/invoice/123",
143-
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36",
144-
"http://localhost:8080/", jsonObject.toString()),
145-
rootle.Grpc("GetInvoice", GrpcCodes.internalError.code, "invoice",
146-
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36",
147-
"http://localhost:8080/", jsonObject.toString())), "billing/user", 0);
148151
```
149152
## Output example
150153
```

config.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package rootle
33
type Config struct {
44
ID *string
55
Application *string
6+
Event *string
67
}
78

89
func NewConfig() *Config {
@@ -18,3 +19,8 @@ func (c *Config) WithApplication(application string) *Config {
1819
c.Application = &application
1920
return c
2021
}
22+
23+
func (c *Config) WithEvent(event string) *Config {
24+
c.Event = &event
25+
return c
26+
}

example/main.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ func main() {
1919

2020
json, _ := json.Marshal(data)
2121

22-
logger.Error("Hello World", rootle.String(string(json)), &rootle.Downstream{
22+
logger.WithEvent(string(json))
23+
24+
logger.Error("Hello World", &rootle.Downstream{
2325
Http: &rootle.Http{
2426
Method: "GET",
2527
StatusCode: rootle.INTERNAL_SERVER_ERROR,

example/main.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@ const logger = new Rootle("ac12Cd-Aevd-12Grx-235f4", "billing-Lambda");
55
logger.info("Info, hello world!");
66
logger.warn("Warn, hello world!");
77

8+
89
const json = {
910
"foo": "bar"
1011
};
1112

12-
logger.error("Error, hello world!", JSON.stringify(json), {
13+
// Can be set in Rootle initlization `new Rootle(id, application, event)`
14+
logger.setEvent(JSON.stringify(json))
15+
16+
logger.error("Error, hello world!", {
1317
http: {
1418
method: "GET",
1519
statusCode: HttpStatusCode.INTERNAL_SERVER_ERROR,

kotlin/gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
version = 0.2.0
1+
version = 0.3.0
22
group = no.telia

kotlin/lib/src/main/kotlin/rootle/Library.kt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package rootle
22

33
import com.google.gson.Gson
4+
import com.google.gson.JsonObject
45

56
enum class GrpcCodes(val code: Int) {
67
Ok(0),
@@ -90,7 +91,7 @@ enum class StatusCode(val code: Int) {
9091
}
9192

9293

93-
class Rootle(private val id: String, private val application: String) {
94+
class Rootle(private val id: String, private val application: String, private var event: String? = null) {
9495

9596
inner class Http (val method: String? = null, val statusCode: Int? = null, val url: String? = null, val useragent: String? = null, val referer: String? = null, val payload: String? = null) {}
9697
inner class Grpc (val procedure: String? = null, val code: Int? = null, val service: String? = null, val useragent: String? = null, val referer: String? = null, val payload: String? = null) {}
@@ -115,11 +116,14 @@ class Rootle(private val id: String, private val application: String) {
115116
println(logJsonString)
116117
}
117118

118-
fun error(message: String, event: String? = null, downstream: Downstream? = null, stackTrace: String? = null, code: Int? = null) {
119+
fun error(message: String, downstream: Downstream? = null, stackTrace: String? = null, code: Int? = null) {
119120
val log = Log(this.id, this.application, System.currentTimeMillis().toString(), message,
120-
"ERROR", event, downstream, stackTrace, code)
121+
"ERROR", this.event, downstream, stackTrace, code)
121122
val logJsonString = Gson().toJson(log)
122123
println(logJsonString)
123124
}
124-
}
125125

126+
fun setEvent(event: String) {
127+
this.event = event
128+
}
129+
}

rootle.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,8 @@ func (c *Config) Warn(message string) {
162162
})
163163
}
164164

165-
func (c *Config) Error(message string, event *string, downstream *Downstream, stackTrace *string, code *int) {
166-
logMessage(*c, message, "ERROR", event, downstream, stackTrace, code, func(logJSON string) {
165+
func (c *Config) Error(message string, downstream *Downstream, stackTrace *string, code *int) {
166+
logMessage(*c, message, "ERROR", c.Event, downstream, stackTrace, code, func(logJSON string) {
167167
log.Println(logJSON)
168168
})
169169
}

typescript/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "rootle",
3-
"version": "0.2.0",
3+
"version": "0.3.0",
44
"description": "TS implementation of Rootle structured log",
55
"main": "dist/rootle.js",
66
"types": "./dist/rootle.d.ts",

typescript/src/rootle.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,16 @@ declare interface Logger {
123123
export default class Rootle {
124124
private id: string;
125125
private application: string;
126+
private event?: string;
126127

127-
constructor(id: string, application: string) {
128+
constructor(id: string, application: string, event?: string) {
128129
this.id = id;
129130
this.application = application;
131+
this.event = event;
130132
}
131133

132134
// @ts-ignore
133-
private logMessage(message: string, level: string, event?: string, downstream?: Downstream, stackTrace?: string, code?: number, callback: Logger) {
135+
private logMessage(message: string, level: string, downstream?: Downstream, stackTrace?: string, code?: number, callback: Logger) {
134136
const log: Log = {
135137
id: this.id,
136138
application: this.application,
@@ -139,7 +141,7 @@ export default class Rootle {
139141
level: level
140142
};
141143
if (level === "ERROR") {
142-
log.event = event;
144+
log.event = this.event;
143145
log.downstream = downstream;
144146
log.stackTrace = stackTrace;
145147
log.code = code;
@@ -149,22 +151,25 @@ export default class Rootle {
149151

150152
}
151153
public info(message: string) {
152-
this.logMessage(message, "INFO", undefined,undefined, undefined, undefined, (logJson) => {
154+
this.logMessage(message, "INFO", undefined, undefined, undefined, (logJson) => {
153155
console.log(logJson);
154156
});
155157
}
156158

157159
public warn(message: string) {
158-
this.logMessage(message, "WARN", undefined, undefined, undefined, undefined, (logJson) => {
160+
this.logMessage(message, "WARN", undefined, undefined, undefined, (logJson) => {
159161
console.log(logJson);
160162
});
161163
}
162164

163-
public error(message: string, event?: string, downstream?: Downstream, stackTrace?: string, code?: number) {
164-
this.logMessage(message, "ERROR", event, downstream, stackTrace, code, (logJson) => {
165+
public error(message: string, downstream?: Downstream, stackTrace?: string, code?: number) {
166+
this.logMessage(message, "ERROR", downstream, stackTrace, code, (logJson) => {
165167
console.log(logJson);
166168
});
167169
}
168170

171+
public setEvent(event: string) {
172+
this.event = event;
173+
}
169174

170175
}

0 commit comments

Comments
 (0)