From d1a86c4d371476ab02cf0ea1400dcd1ca387b0fa Mon Sep 17 00:00:00 2001 From: Hrishikesh Patil Date: Thu, 19 Oct 2023 12:46:34 +0530 Subject: [PATCH 1/6] documentation updated --- client/README.md | 70 ++++++++++++++++++++++++++++++++++-------------- 1 file changed, 50 insertions(+), 20 deletions(-) diff --git a/client/README.md b/client/README.md index 310f13c..e60ec9b 100644 --- a/client/README.md +++ b/client/README.md @@ -73,42 +73,71 @@ The above piece of code will end up printing something like the following (the d 2023/10/15 21:57:41 INFO Println message ``` -## Printing logs via standard output +### Redirecting the output to a file -Bark client, as shown above, is capable of sending logs to a server as well as printing them to the standard output as well. It can also do both of those things simultaneously. The architecture in very simple representation looks like this: +> [!IMPORTANT] +This section outlines the behavior of log printing when `enable_slog` is enabled during client initialization. It's important to note that this configuration does not affect how logs are sent to the Bark database. The `NewClient` and `NewClientWithServer` functions are designed to transmit logs to the Bark server or database. If you prefer not to send logs to the server, consider using the `NewSloggerClient` function. -![barkslogger.svg](../_nocode/images/barkslogger.svg) - -You can use any of the log methods available in bark client to do this. If you want to print the logs to a different output, such as a file, you can use the `WithCustomOut` method. This method takes an `io.Writer` parameter and sets it as the output writer for the bark client. For example, if you want to print the logs to a file named random.txt, you can do this: +To reroute logs to a file instead of the console (assuming `enable_slog` is set to true during client initialization), you can use the `WithCustomOut` method, accepting an `io.Writer` parameter. +Here's a sample usage: ```go -barkClient := client.NewClient("", "", "", "", - "", "") +logger := client.NewClient("", "", "", "", true, "") -file, _ := os.Create("random.txt") +file, _ := os.Create("") -logClient.WithCustomOut(file) +log.WithCustomOut(file) -barkClient.Info("Some Message that'll be send to random.txt file") +log.Panic("Panic message") +log.Alert("Alert message", true) +log.Error("Error message") +log.Warn("Warn message") +log.Notice("Notice message") +log.Info("Info message") +log.Debug("Debug message") +log.Println("Println message") +``` + +This code configures a logger to direct log output to a designated file, showcasing multiple log levels. + +The above piece of code will end up printing to the specified file, something like the following (the dates in the beginning of each line will vary): +``` +2023/10/15 21:57:41 PANIC Panic message +2023/10/15 21:57:41 ALERT Alert message +2023/10/15 21:57:41 ERROR Error message +2023/10/15 21:57:41 WARN Warn message +2023/10/15 21:57:41 NOTICE Notice message +2023/10/15 21:57:41 INFO Info message +2023/10/15 21:57:41 DEBUG Debug message +2023/10/15 21:57:41 INFO Println message ``` -### Slog +### Handling output with slog Handlers + +> [!IMPORTANT] +This section outlines the behavior of log printing when `enable_slog` is enabled during client initialization. It's important to note that this configuration does not affect how logs are sent to the Bark database. The `NewClient` and `NewClientWithServer` functions are designed to transmit logs to the Bark server or database. If you prefer not to send logs to the server, consider using the `NewSloggerClient` function. + Bark client uses [slog](https://go.dev/blog/slog) internally to handle the printing of the logs. Slog is a simple and structured logging library that comes with Go (version 1.21+). You can customize how slog prints the logs by specifying a [handler](https://pkg.go.dev/log/slog#Handler). A handler is a function that takes a log record and writes it to an output. Slog provides some built-in handlers, such as [JSONHandler](https://pkg.go.dev/log/slog#JSONHandler) and [TextHandler](https://pkg.go.dev/log/slog#TextHandler), or you can write your own. -**_Note:_** Changing the handler will only affect how the logs are printed, not how they are sent to bark server. - To specify a handler for the bark client, you can use the `WithSlogHandler` method. This method takes a `handler` function as a parameter and sets it as the handler for the slog logger. For example, if you want to use the `JSONHandler` and print the logs as JSON objects to a file named `random.txt`, you can do this: -``` -barkClient := client.NewClient("", "", "", "") +```go +log := client.NewClient("", "", "", "", true, "") -file, _ := os.Create("random.txt") +file, _ := os.Create("") -logClient.WithSlogHandler(slog.NewJSONHandler(file, barkslogger.Options())) +log.WithSlogHandler(slog.NewJSONHandler(file, barkslogger.Options())) -barkClient.Info("Some Message that'll be send to random.txt file") +log.Panic("Panic message") +log.Alert("Alert message", true) +log.Error("Error message") +log.Warn("Warn message") +log.Notice("Notice message") +log.Info("Info message") +log.Debug("Debug message") +log.Println("Println message") ``` You may have noticed that we are passing some options to the `JSONHandler` using the `barkslogger.Options()` method. This is because slog has predefined labels for only four log levels: `info, warning, debug, and error`. However, bark client supports three additional log levels: `alert, panic, and notice`. The options returned by `barkslogger.Options()` define labels for these additional log levels. @@ -116,15 +145,16 @@ If you add a nil options, the log labels will appear as described in the [slog d [Slog treats log levels as integers](https://pkg.go.dev/log/slog#Level). The predefined log levels have the following values: +> [!] > LevelDebug Level = -4 \ > LevelInfo Level = 0 \ > LevelWarn Level = 4 \ -> LevelError Level = 8 \ +> LevelError Level = 8 The custom log levels defined by bark client have the following values: ``` -Notice = 1 +Notice = 3 Alert = 9 Panic = 10 ``` From af7ee8b56c054b1bdd5fa9a2c1357fe4152d23e3 Mon Sep 17 00:00:00 2001 From: Hrishikesh Patil Date: Thu, 19 Oct 2023 12:58:56 +0530 Subject: [PATCH 2/6] doc updated --- client/README.md | 117 ++++++++++++++++++++++++----------------------- 1 file changed, 61 insertions(+), 56 deletions(-) diff --git a/client/README.md b/client/README.md index e60ec9b..77d795a 100644 --- a/client/README.md +++ b/client/README.md @@ -2,7 +2,7 @@ ## IMPORTANT: It is not Yet Built -## What does the client do? +## What does the client do? The Bark client (just _client_ henceforth) is the _library_ side of the bark. It is the piece that takes in the logs from any golang program and sends it to the server which is configured against the client. It is supposed to have the utility functions to help users log to bark directly from go code without having to worry about network calls and such. ## Levels of Logs @@ -19,11 +19,11 @@ The client defines 7 levels of logs: Any single character in the place of error level in a parsable single log message would indicate the level of **INFO**. ## Simple usecase -The client can be initialized and used as follows (we explain the options below code sample): +The client can be initialized and used as follows (we explain the options below code sample): ```go -barkClient := client.NewClient("", "", "", "", - "") +barkClient := client.NewClient("", "", "", "", +"") barkClient.Panic("E#1LPW24 - Panic message") barkClient.Alert("E#1LPW25 - Alert message", false) @@ -41,12 +41,12 @@ The options that are used for initializing the client are as follows: - **bark_server_url**: This is the URL of a running bark server. It must end in `/`. For example `http://bark.example.com/` or `http://127.0.0.1:8080/` - **default_log_level**: When you use `Println` or `Default`, the log message is parsed (rules for prasing are described [here](../_nocode/docs/log-string-parsing-in-bark.md)) and if it does not contain any indication for what the log level is, then the value supplied in this field is used as the log level for sent log message. When using dedicated methods for error levels (e.g. `Panic`, `Error` etc.), the parsed error level is overwritten. - **default_service_name**: This is the name of the service which is sending the log - so it has to be the name of the program or service which is calling it. In case a blank string is sent, the value against `constants.DefaultLogServiceName` (currently set to `def_svc`) is used. -- **session_name**: This is the name of the calling app's session. This value is supposed to indicate which instance among possibly multiple instances of a service sent a log message. For example, in case of the service being deployed within Kubernetes, it might indicate the service's pod's name. If the value is sent as a blank string, client will try to use the machine's hostname. If it fails to fetch the hostname, a random string will be used instead. +- **session_name**: This is the name of the calling app's session. This value is supposed to indicate which instance among possibly multiple instances of a service sent a log message. For example, in case of the service being deployed within Kubernetes, it might indicate the service's pod's name. If the value is sent as a blank string, client will try to use the machine's hostname. If it fails to fetch the hostname, a random string will be used instead. - **enable_slog**: This enables [slog](https://go.dev/blog/slog) for the client. When this option is enabled, all logs in addition to being sent to the bark server is also printed on STDOUT of the service. - **enable_bulk_dispatch**: Setting this to true would enable the client to push all the requests being received in a channel and start using it. It improves the overall performance of the client sending log entries to the server. ### Simplest usecase (without any server) -The simplest usecase of any logging library is to print to STDOUT. While the primary usecase of bark is to be able to dispatch log messages to a remote server, when we start off with a new project, we often just want to start logging things to STDOUT. Maybe even later, that is how we want to use logs. For such usecases, the client library offers `NewSloggerClient` which uses the built in [slog](https://go.dev/blog/slog) package in go (version 1.21+) to log your messages to STDOUT with levels. Example: +The simplest usecase of any logging library is to print to STDOUT. While the primary usecase of bark is to be able to dispatch log messages to a remote server, when we start off with a new project, we often just want to start logging things to STDOUT. Maybe even later, that is how we want to use logs. For such usecases, the client library offers `NewSloggerClient` which uses the built in [slog](https://go.dev/blog/slog) package in go (version 1.21+) to log your messages to STDOUT with levels. Example: ```go log := client.NewSloggerClient("INFO") @@ -60,7 +60,7 @@ log.Info("Info message") log.Debug("Debug message") log.Println("Println message") ``` -The above piece of code will end up printing something like the following (the dates in the beginning of each line will vary): +The above piece of code will end up printing something like the following (the dates in the beginning of each line will vary): ``` 2023/10/15 21:57:41 PANIC Panic message @@ -73,83 +73,87 @@ The above piece of code will end up printing something like the following (the d 2023/10/15 21:57:41 INFO Println message ``` -### Redirecting the output to a file +## Printing logs to a file +Bark client, as shown above, is capable of sending logs to a server as well as printing them to the standard output as well. It can also do both of those things simultaneously. The architecture in very simple representation looks like this: -> [!IMPORTANT] -This section outlines the behavior of log printing when `enable_slog` is enabled during client initialization. It's important to note that this configuration does not affect how logs are sent to the Bark database. The `NewClient` and `NewClientWithServer` functions are designed to transmit logs to the Bark server or database. If you prefer not to send logs to the server, consider using the `NewSloggerClient` function. +![barkslogger.svg](../_nocode/images/barkslogger.svg) +You can use any of the log methods available in bark client to do this. If you want to print the logs to a different output, such as a file, you can use the `SetCustomOut` method. This method takes an `io.Writer` parameter and sets it as the output writer for the bark client. For example, if you want to print the logs to a file named random.txt, you can do this: -To reroute logs to a file instead of the console (assuming `enable_slog` is set to true during client initialization), you can use the `WithCustomOut` method, accepting an `io.Writer` parameter. -Here's a sample usage: ```go -logger := client.NewClient("", "", "", "", true, "") +log := client.NewClient("http://127.0.0.1:8080/", "INFO", "BarkClientFileTest", "TestClientSession", true, false) -file, _ := os.Create("") +file, err := os.Create("random.txt") +if err != nil { + fmt.Println("Error when creating new file: ", err) + return +} -log.WithCustomOut(file) +log.SetCustomOut(file) -log.Panic("Panic message") -log.Alert("Alert message", true) -log.Error("Error message") -log.Warn("Warn message") -log.Notice("Notice message") -log.Info("Info message") -log.Debug("Debug message") -log.Println("Println message") +log.Info("Some Message that'll be sent to random.txt file") +log.WaitAndEnd() ``` +The above code will write the output to `random.txt` file. You can expect the file to contain something like this: -This code configures a logger to direct log output to a designated file, showcasing multiple log levels. - -The above piece of code will end up printing to the specified file, something like the following (the dates in the beginning of each line will vary): +```text +2023/10/18 19:27:51 INFO Some Message that'll be sent to random.txt file ``` -2023/10/15 21:57:41 PANIC Panic message -2023/10/15 21:57:41 ALERT Alert message -2023/10/15 21:57:41 ERROR Error message -2023/10/15 21:57:41 WARN Warn message -2023/10/15 21:57:41 NOTICE Notice message -2023/10/15 21:57:41 INFO Info message -2023/10/15 21:57:41 DEBUG Debug message -2023/10/15 21:57:41 INFO Println message -``` - -### Handling output with slog Handlers - -> [!IMPORTANT] -This section outlines the behavior of log printing when `enable_slog` is enabled during client initialization. It's important to note that this configuration does not affect how logs are sent to the Bark database. The `NewClient` and `NewClientWithServer` functions are designed to transmit logs to the Bark server or database. If you prefer not to send logs to the server, consider using the `NewSloggerClient` function. +### Slog and writing to a file Bark client uses [slog](https://go.dev/blog/slog) internally to handle the printing of the logs. Slog is a simple and structured logging library that comes with Go (version 1.21+). You can customize how slog prints the logs by specifying a [handler](https://pkg.go.dev/log/slog#Handler). A handler is a function that takes a log record and writes it to an output. Slog provides some built-in handlers, such as [JSONHandler](https://pkg.go.dev/log/slog#JSONHandler) and [TextHandler](https://pkg.go.dev/log/slog#TextHandler), or you can write your own. -To specify a handler for the bark client, you can use the `WithSlogHandler` method. This method takes a `handler` function as a parameter and sets it as the handler for the slog logger. For example, if you want to use the `JSONHandler` and print the logs as JSON objects to a file named `random.txt`, you can do this: -```go -log := client.NewClient("", "", "", "", true, "") +**_Note:_** Changing the handler will only affect how the logs are printed, not how they are sent to bark server. -file, _ := os.Create("") +To specify a handler for the bark client, you can use the `SetSlogHandler` method. This method takes a `handler` function as a parameter and sets it as the handler for the slog logger. For example, if you want to use the `JSONHandler` and print the logs as JSON objects to a file named `random.txt`, you can do this: -log.WithSlogHandler(slog.NewJSONHandler(file, barkslogger.Options())) +```go +package main + +import ( + "fmt" + "github.com/techrail/bark/client" + "log/slog" + "os" +) + +func main() { + log := client.NewClient("http://127.0.0.1:8080/", "INFO", "BarkClientFileTest", "TestClientSession", true, false) + + file, err := os.Create("random.txt") + if err != nil { + fmt.Println("E#1M5WRN - Error when creating new file: ", err) + return + } + + // We are using JSONHandler here so the line that will be logged will actually be a JSON string + log.SetSlogHandler(slog.NewJSONHandler(file, client.SlogHandlerOptions())) + // If you want to log to STDOUT, you can use `os.Stdout` in place of the `file` as writer + // Of course in case that you would have remove the unused code from above. + + log.Info("Some Message that'll be sent to random.txt file") + log.WaitAndEnd() +} +``` +You can expect the `random.txt` file to contain something like this with different time being logged (we are using a JSON handler for slog): -log.Panic("Panic message") -log.Alert("Alert message", true) -log.Error("Error message") -log.Warn("Warn message") -log.Notice("Notice message") -log.Info("Info message") -log.Debug("Debug message") -log.Println("Println message") +```text +{"time":"2023-10-18T19:30:38.773512+05:30","level":"INFO","msg":"Some Message that'll be sent to random.txt file"} ``` -You may have noticed that we are passing some options to the `JSONHandler` using the `barkslogger.Options()` method. This is because slog has predefined labels for only four log levels: `info, warning, debug, and error`. However, bark client supports three additional log levels: `alert, panic, and notice`. The options returned by `barkslogger.Options()` define labels for these additional log levels. + +You may have noticed that we are passing some options to the `JSONHandler` using the `client.SlogHandlerOptions()` method. This is because slog has predefined labels for only four log levels: `info, warning, debug, and error`. However, bark client supports three additional log levels: `alert, panic, and notice`. The options returned by `client.SlogHandlerOptions()` define labels for these additional log levels. If you add a nil options, the log labels will appear as described in the [slog documentation here](https://pkg.go.dev/log/slog#Level.String) [Slog treats log levels as integers](https://pkg.go.dev/log/slog#Level). The predefined log levels have the following values: -> [!] > LevelDebug Level = -4 \ > LevelInfo Level = 0 \ > LevelWarn Level = 4 \ -> LevelError Level = 8 +> LevelError Level = 8 \ The custom log levels defined by bark client have the following values: @@ -158,4 +162,5 @@ Notice = 3 Alert = 9 Panic = 10 ``` + If you are writing a custom handler for slog, please make sure to handle these log levels appropriately. \ No newline at end of file From 882aae7f4ef7ea531dbcdd79ca48908ba20000bf Mon Sep 17 00:00:00 2001 From: Hrishikesh Patil Date: Sat, 4 Nov 2023 20:49:09 +0530 Subject: [PATCH 3/6] json slogger function added --- client/README.md | 36 ++++++++++++++++++++++-- client/client.go | 9 ++++++ cmd/examples/client_json_slogger/main.go | 17 +++++++++++ cmd/examples/client_wait/main.go | 14 ++++----- 4 files changed, 66 insertions(+), 10 deletions(-) create mode 100644 cmd/examples/client_json_slogger/main.go diff --git a/client/README.md b/client/README.md index 38f2441..9d8467e 100644 --- a/client/README.md +++ b/client/README.md @@ -73,6 +73,36 @@ The above piece of code will end up printing something like the following (the d 2023/10/15 21:57:41 INFO Println message ``` +### Printing logs in JSON format +If you want the logs to be printed in JSON format you can use `NewClientWithJSONSlogger` method from a client as shown below, + + +```go +log := client.NewClientWithJSONSlogger("http://127.0.0.1:8080/", "INFO", "BarkClientFileTest", "TestClientSession", false) + +log.Info("1N09FW - This is an Info message!") +log.Debug("1N09GG - This is an Debug message!") +log.Warn("1N09H5 - This is an Warn message!") +log.Notice("1N09HL - This is an Notice message!") +log.Error("1N09HT - This is an Error message!") +log.Panic("1N09I7 - This is an Panic message!") +log.Alert("1N09IG - This is an Alert message!", false) +log.Default("I#1N09JH - This is an Default message!") +log.Println("D#1N09JR - This is an Print message!") +``` +The above piece of code will end up printing something like the following (the dates in the beginning of each line will vary): +``` +{"time":"2023-11-04T20:35:35.0472358+05:30","level":"INFO","msg":"1N09FW - This is an Info message!"} +{"time":"2023-11-04T20:35:35.0510558+05:30","level":"DEBUG","msg":"1N09GG - This is an Debug message!"} +{"time":"2023-11-04T20:35:35.0572919+05:30","level":"WARN","msg":"1N09H5 - This is an Warn message!"} +{"time":"2023-11-04T20:35:35.0578286+05:30","level":"NOTICE","msg":"1N09HL - This is an Notice message!"} +{"time":"2023-11-04T20:35:35.0580332+05:30","level":"ERROR","msg":"1N09HT - This is an Error message!"} +{"time":"2023-11-04T20:35:35.0586284+05:30","level":"PANIC","msg":"1N09I7 - This is an Panic message!"} +{"time":"2023-11-04T20:35:35.0586284+05:30","level":"ALERT","msg":"1N09IG - This is an Alert message!"} +{"time":"2023-11-04T20:35:35.0591469+05:30","level":"INFO","msg":"I#1N09JH - This is an Default message!"} +{"time":"2023-11-04T20:35:35.059302+05:30","level":"DEBUG","msg":"D#1N09JR - This is an Default message!"} +``` + ## Printing logs to a file Bark client, as shown above, is capable of sending logs to a server as well as printing them to the standard output as well. It can also do both of those things simultaneously. The architecture in very simple representation looks like this: @@ -131,9 +161,9 @@ func main() { // We are using JSONHandler here so the line that will be logged will actually be a JSON string log.SetSlogHandler(slog.NewJSONHandler(file, client.SlogHandlerOptions())) + // If you want to log to STDOUT, you can use `os.Stdout` in place of the `file` as writer - // Of course in case that you would have remove the unused code from above. - + // Of course in case that you would have removed the unused code from above. log.Info("Some Message that'll be sent to random.txt file") log.WaitAndEnd() } @@ -153,7 +183,7 @@ If you add a nil options, the log labels will appear as described in the [slog d > LevelDebug Level = -4 \ > LevelInfo Level = 0 \ > LevelWarn Level = 4 \ -> LevelError Level = 8 \ +> LevelError Level = 8 The custom log levels defined by bark client have the following values: diff --git a/client/client.go b/client/client.go index e4ed8f7..db018a3 100644 --- a/client/client.go +++ b/client/client.go @@ -675,6 +675,15 @@ func NewClientWithServer(dbUrl, defaultLogLvl, svcName, svcInstName string, enab } } +// NewClientWithJSONSlogger returns a Config object that will print logs to stdout in JSON format. +// *Config returned by NewClientWithJSONSlogger behaves the same as NewClient, but it prints the logs in JSON. +// NewClientWithJSONSlogger accepts the same parameters as NewClient except enableSlog as it is enabled by default. +func NewClientWithJSONSlogger(url, defaultLogLvl, svcName, svcInstName string, enableBulkSend bool) *Config { + client := NewClient(url, defaultLogLvl, svcName, svcInstName, true, enableBulkSend) + client.SetSlogHandler(slog.NewJSONHandler(os.Stdout, SlogHandlerOptions())) + return client +} + // SetCustomOut allows users to set output to custom writer instead of the default standard output func (c *Config) SetCustomOut(out io.Writer) { c.Slogger = newSlogger(out) diff --git a/cmd/examples/client_json_slogger/main.go b/cmd/examples/client_json_slogger/main.go new file mode 100644 index 0000000..cbd283d --- /dev/null +++ b/cmd/examples/client_json_slogger/main.go @@ -0,0 +1,17 @@ +package main + +import "github.com/techrail/bark/client" + +func main() { + log := client.NewClientWithJSONSlogger("http://127.0.0.1:8080/", "INFO", "BarkClientFileTest", "TestClientSession", false) + log.Info("1N09FW - This is an Info message!") + log.Debug("1N09GG - This is an Debug message!") + log.Warn("1N09H5 - This is an Warn message!") + log.Notice("1N09HL - This is an Notice message!") + log.Error("1N09HT - This is an Error message!") + log.Panic("1N09I7 - This is an Panic message!") + log.Alert("1N09IG - This is an Alert message!", false) + log.Default("I#1N09JH - This is an Default message!") + log.Println("D#1N09JR - This is an Print message!") + log.WaitAndEnd() +} diff --git a/cmd/examples/client_wait/main.go b/cmd/examples/client_wait/main.go index 2f9b707..6224a95 100644 --- a/cmd/examples/client_wait/main.go +++ b/cmd/examples/client_wait/main.go @@ -66,13 +66,13 @@ func main() { log.Println("1LPWC7 - Default message") _ = log.Raw(client.RawLog{ - LogTime: time.Now().UTC(), - LogLevel: "INFO", - ServiceName: "Bark Test", - SessionName: "TestSessionWait1", - Code: "1M2U4J", - Message: "Testing raw log", - MoreData: nil, + LogTime: time.Now().UTC(), + LogLevel: "INFO", + ServiceName: "Bark Test", + ServiceInstanceName: "TestSessionWait1", + Code: "1M2U4J", + Message: "Testing raw log", + MoreData: nil, }, false) log.WaitAndEnd() From 8588532762f2982c152e6c4c4a1987a0e2d39f51 Mon Sep 17 00:00:00 2001 From: Hrishikesh Patil Date: Sat, 4 Nov 2023 21:02:26 +0530 Subject: [PATCH 4/6] NewClientWithServerAndJSONSlogger function added --- client/client.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/client/client.go b/client/client.go index db018a3..03b4868 100644 --- a/client/client.go +++ b/client/client.go @@ -684,6 +684,15 @@ func NewClientWithJSONSlogger(url, defaultLogLvl, svcName, svcInstName string, e return client } +// NewClientWithServerAndJSONSlogger returns a Config object that will print logs to stdout in JSON format. +// *Config returned by NewClientWithJSONSlogger behaves the same as NewClientWithServer, but it prints the logs in JSON. +// NewClientWithServerAndJSONSlogger accepts the same parameters as NewClientWithServer except enableSlog as it is enabled by default. +func NewClientWithServerAndJSONSlogger(dbUrl, defaultLogLvl, svcName, svcInstName string) *Config { + client := NewClientWithServer(dbUrl, defaultLogLvl, svcName, svcInstName, true) + client.SetSlogHandler(slog.NewJSONHandler(os.Stdout, SlogHandlerOptions())) + return client +} + // SetCustomOut allows users to set output to custom writer instead of the default standard output func (c *Config) SetCustomOut(out io.Writer) { c.Slogger = newSlogger(out) From 22544edba82cf9b76a1fbf7356abb8db0e932554 Mon Sep 17 00:00:00 2001 From: Hrishikesh Patil Date: Mon, 6 Nov 2023 20:53:11 +0530 Subject: [PATCH 5/6] json func changed --- client/README.md | 5 ++--- client/client.go | 19 +++++-------------- cmd/examples/client_json_slogger/main.go | 8 +++++--- 3 files changed, 12 insertions(+), 20 deletions(-) diff --git a/client/README.md b/client/README.md index 9d8467e..725b49c 100644 --- a/client/README.md +++ b/client/README.md @@ -74,12 +74,11 @@ The above piece of code will end up printing something like the following (the d ``` ### Printing logs in JSON format -If you want the logs to be printed in JSON format you can use `NewClientWithJSONSlogger` method from a client as shown below, +If you just want the logs to be printed in JSON format you can use `NewSloggerClientJson` method from a client as shown below, ```go -log := client.NewClientWithJSONSlogger("http://127.0.0.1:8080/", "INFO", "BarkClientFileTest", "TestClientSession", false) - +log := client.NewSloggerClientJson(constants.Info) log.Info("1N09FW - This is an Info message!") log.Debug("1N09GG - This is an Debug message!") log.Warn("1N09H5 - This is an Warn message!") diff --git a/client/client.go b/client/client.go index 03b4868..f418c26 100644 --- a/client/client.go +++ b/client/client.go @@ -675,20 +675,11 @@ func NewClientWithServer(dbUrl, defaultLogLvl, svcName, svcInstName string, enab } } -// NewClientWithJSONSlogger returns a Config object that will print logs to stdout in JSON format. -// *Config returned by NewClientWithJSONSlogger behaves the same as NewClient, but it prints the logs in JSON. -// NewClientWithJSONSlogger accepts the same parameters as NewClient except enableSlog as it is enabled by default. -func NewClientWithJSONSlogger(url, defaultLogLvl, svcName, svcInstName string, enableBulkSend bool) *Config { - client := NewClient(url, defaultLogLvl, svcName, svcInstName, true, enableBulkSend) - client.SetSlogHandler(slog.NewJSONHandler(os.Stdout, SlogHandlerOptions())) - return client -} - -// NewClientWithServerAndJSONSlogger returns a Config object that will print logs to stdout in JSON format. -// *Config returned by NewClientWithJSONSlogger behaves the same as NewClientWithServer, but it prints the logs in JSON. -// NewClientWithServerAndJSONSlogger accepts the same parameters as NewClientWithServer except enableSlog as it is enabled by default. -func NewClientWithServerAndJSONSlogger(dbUrl, defaultLogLvl, svcName, svcInstName string) *Config { - client := NewClientWithServer(dbUrl, defaultLogLvl, svcName, svcInstName, true) +// NewSloggerClientJson returns a Config object that will print logs to stdout in JSON format. +// *Config returned by NewSloggerClientJson behaves the same as NewSloggerClient, but it prints the logs in JSON. +// NewClientWithJSONSlogger accepts the same parameters as NewSloggerClient. +func NewSloggerClientJson(defaultLogLvl string) *Config { + client := NewSloggerClient(defaultLogLvl) client.SetSlogHandler(slog.NewJSONHandler(os.Stdout, SlogHandlerOptions())) return client } diff --git a/cmd/examples/client_json_slogger/main.go b/cmd/examples/client_json_slogger/main.go index cbd283d..79be29a 100644 --- a/cmd/examples/client_json_slogger/main.go +++ b/cmd/examples/client_json_slogger/main.go @@ -1,9 +1,12 @@ package main -import "github.com/techrail/bark/client" +import ( + "github.com/techrail/bark/client" + "github.com/techrail/bark/constants" +) func main() { - log := client.NewClientWithJSONSlogger("http://127.0.0.1:8080/", "INFO", "BarkClientFileTest", "TestClientSession", false) + log := client.NewSloggerClientJson(constants.Info) log.Info("1N09FW - This is an Info message!") log.Debug("1N09GG - This is an Debug message!") log.Warn("1N09H5 - This is an Warn message!") @@ -13,5 +16,4 @@ func main() { log.Alert("1N09IG - This is an Alert message!", false) log.Default("I#1N09JH - This is an Default message!") log.Println("D#1N09JR - This is an Print message!") - log.WaitAndEnd() } From 796332c282993d2d5c88cb333c188a315b5e1cdf Mon Sep 17 00:00:00 2001 From: Hrishikesh Patil Date: Tue, 7 Nov 2023 11:11:43 +0530 Subject: [PATCH 6/6] change to instance from session --- client/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/README.md b/client/README.md index 725b49c..6564f64 100644 --- a/client/README.md +++ b/client/README.md @@ -41,7 +41,7 @@ The options that are used for initializing the client are as follows: - **bark_server_url**: This is the URL of a running bark server. It must end in `/`. For example `http://bark.example.com/` or `http://127.0.0.1:8080/` - **default_log_level**: When you use `Println` or `Default`, the log message is parsed (rules for prasing are described [here](../_nocode/docs/log-string-parsing-in-bark.md)) and if it does not contain any indication for what the log level is, then the value supplied in this field is used as the log level for sent log message. When using dedicated methods for error levels (e.g. `Panic`, `Error` etc.), the parsed error level is overwritten. - **default_service_name**: This is the name of the service which is sending the log - so it has to be the name of the program or service which is calling it. In case a blank string is sent, the value against `constants.DefaultLogServiceName` (currently set to `def_svc`) is used. -- **service_instance_name**: This is the name of the calling app's session. This value is supposed to indicate which instance among possibly multiple instances of a service sent a log message. For example, in case of the service being deployed within Kubernetes, it might indicate the service's pod's name. If the value is sent as a blank string, client will try to use the machine's hostname. If it fails to fetch the hostname, a random string will be used instead. +- **service_instance_name**: This is the name of the calling app's instance. This value is supposed to indicate which instance among possibly multiple instances of a service sent a log message. For example, in case of the service being deployed within Kubernetes, it might indicate the service's pod's name. If the value is sent as a blank string, client will try to use the machine's hostname. If it fails to fetch the hostname, a random string will be used instead. - **enable_slog**: This enables [slog](https://go.dev/blog/slog) for the client. When this option is enabled, all logs in addition to being sent to the bark server is also printed on STDOUT of the service. - **enable_bulk_dispatch**: Setting this to true would enable the client to push all the requests being received in a channel and start using it. It improves the overall performance of the client sending log entries to the server.