Skip to content

Commit 5b1f858

Browse files
Remove the usage of jsonObject from bark and handle more_data as plain string
2 parents d43b3c4 + 02d56bb commit 5b1f858

8 files changed

Lines changed: 182 additions & 19 deletions

File tree

_nocode/db/migrations/001_base_schema.up.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
CREATE TABLE IF NOT EXISTS app_log
1+
CREATE TABLE IF NOT EXISTS audit.app_log
22
(
33
id BIGSERIAL PRIMARY KEY,
44
log_time TIMESTAMP WITHOUT TIME ZONE NOT NULL DEFAULT (NOW() AT TIME ZONE ('utc')),

client/client.go

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@ import (
44
"context"
55
`encoding/json`
66
"fmt"
7+
"github.com/techrail/bark/config"
8+
"github.com/techrail/bark/resources"
9+
"github.com/techrail/bark/services/dbLogWriter"
10+
"github.com/techrail/bark/services/ingestion"
11+
"github.com/techrail/bark/utils"
712
"io"
813
"log/slog"
914
"os"
1015
"strings"
1116
"time"
1217

13-
"github.com/techrail/bark/resources"
14-
"github.com/techrail/bark/services/dbLogWriter"
15-
"github.com/techrail/bark/services/ingestion"
16-
"github.com/techrail/bark/utils"
17-
1818
"github.com/techrail/bark/appRuntime"
1919
"github.com/techrail/bark/constants"
2020
"github.com/techrail/bark/models"
@@ -607,13 +607,29 @@ func NewClient(url, defaultLogLvl, svcName, svcInstName string, enableSlog bool,
607607
}
608608
}
609609

610-
// NewClientWithServer returns a client config which performs the job of the server as well
610+
// NewClientWithServer returns a client config which performs the job of the server as well.
611+
// It is a wrapper around NewDirectToDbClientCustomSchemaTable with schema name set to a blank string
612+
func NewClientWithServer(dbUrl, defaultLogLvl, svcName, svcInstName string, enableSlog bool) *Config {
613+
return NewDirectToDbClient(dbUrl, defaultLogLvl, svcName, svcInstName, enableSlog)
614+
}
615+
616+
func NewDirectToDbClient(dbUrl, defaultLogLvl, svcName, svcInstName string, enableSlog bool) *Config {
617+
return NewDirectToDbClientCustomSchemaTable(dbUrl, "", "", defaultLogLvl, svcName, svcInstName, enableSlog)
618+
}
619+
620+
// NewDirectToDbClientCustomSchemaTable returns a client config which performs the job of the server as well
611621
// It differs from NewClient in two main ways: it does not have the option to do bulk inserts (they are not needed)
612622
// and it accepts the database URL instead of server URL.
613623
//
614-
// The url parameter is the database URL where the logs will be stored.
624+
// The dbUrl parameter is the database URL where the logs will be stored.
615625
// It must be a valid postgresql protocol string.
616626
//
627+
// The schemaName is the name of the schema (optional) where the postgresql table resides.
628+
// If the table is accessible without a schema name using the dbUrl connection, this can be left blank.
629+
//
630+
// The tableName is the name of the table where the logs are to be stored.
631+
// This is supposed to be set if your table is named anything other than `app_log`.
632+
//
617633
// The defaultLogLvl parameter is the log level for logging. It must be one of the constants
618634
// defined in the constants package, such as INFO, WARN, ERROR, etc. If an invalid value
619635
// is given, the function will print a warning message and use INFO as the default level.
@@ -629,7 +645,7 @@ func NewClient(url, defaultLogLvl, svcName, svcInstName string, enableSlog bool,
629645
// The enableSlog parameter is a boolean flag that indicates whether to enable slog logging
630646
// to standard output. If true, the function will create and assign a new slog.Logger object
631647
// to the Config object. If false, the Config object will have a nil Slogger field.
632-
func NewClientWithServer(dbUrl, defaultLogLvl, svcName, svcInstName string, enableSlog bool) *Config {
648+
func NewDirectToDbClientCustomSchemaTable(dbUrl, schemaName, tableName, defaultLogLvl, svcName, svcInstName string, enableSlog bool) *Config {
633649
if !isValid(defaultLogLvl) {
634650
fmt.Printf("L#1M1XXN - %v is not an acceptable log level. %v will be used as the default log level", defaultLogLvl, constants.DefaultLogLevel)
635651
defaultLogLvl = constants.DefaultLogLevel
@@ -645,6 +661,9 @@ func NewClientWithServer(dbUrl, defaultLogLvl, svcName, svcInstName string, enab
645661
fmt.Printf("L#1M1XZH - Blank instance name supplied. Using %v as Service Instance Name", svcInstName)
646662
}
647663

664+
config.SetDbSchemaName(schemaName)
665+
config.SetDbTableName(tableName)
666+
648667
var slogger *slog.Logger
649668

650669
if enableSlog {
@@ -667,7 +686,7 @@ func NewClientWithServer(dbUrl, defaultLogLvl, svcName, svcInstName string, enab
667686
bld := models.NewBarkLogDao()
668687
err = bld.InsertServerStartedLog()
669688
if err != nil {
670-
panic("P#1LQ2YQ - Bark server start failed: " + err.Error())
689+
panic("P#20JFKK - Bark server start failed: " + err.Error())
671690
}
672691

673692
// Start the server side

cmd/examples/client_embedded_test/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ func main() {
1212
fmt.Println("This")
1313
return
1414
}
15-
log := client.NewClientWithServer("postgres://vkaushal288:vkaushal288@127.0.0.1:5432/bark", "INFO", "brktest", "load test session", false)
15+
log := client.NewClientWithServer("postgres://vaibhav:vaibhav@127.0.0.1:5432/bark", "INFO", "brktest", "load test session", false)
1616

1717
for i := 0; i < 500; i++ {
1818
log.Printf("1M2UBT - Default message - %v", i)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"github.com/techrail/bark/client"
6+
"os"
7+
)
8+
9+
func main() {
10+
err := os.Setenv("DEBUG", "true")
11+
if err != nil {
12+
fmt.Println("This")
13+
return
14+
}
15+
log := client.NewDirectToDbClientCustomSchemaTable("postgres://vaibhav:vaibhav@127.0.0.1:5432/bark", "audit", "", "INFO", "brktest", "load test session", false)
16+
17+
for i := 0; i < 500; i++ {
18+
log.Printf("20JFH1 - schema default message - %v", i)
19+
}
20+
21+
// Now we will disable the Debug printing
22+
log.DisableDebugLogs()
23+
log.Debug("20JF30 - This message should not be saved")
24+
log.EnableDebugLogs()
25+
log.Debug("20JF3P - This message should be saved in the table in audit schema")
26+
27+
log.WaitAndEnd()
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"github.com/techrail/bark/client"
6+
"os"
7+
)
8+
9+
func main() {
10+
err := os.Setenv("DEBUG", "true")
11+
if err != nil {
12+
fmt.Println("This")
13+
return
14+
}
15+
log := client.NewDirectToDbClientCustomSchemaTable("postgres://vaibhav:vaibhav@127.0.0.1:5432/bark", "audit", "app_log2", "INFO", "brktest", "load test session", false)
16+
17+
for i := 0; i < 500; i++ {
18+
log.Printf("I#20XJRQ - schema default message - %v", i)
19+
}
20+
21+
// Now we will disable the Debug printing
22+
log.DisableDebugLogs()
23+
log.Debug("20XJSP - This message should not be saved")
24+
log.EnableDebugLogs()
25+
log.Debug("20XJSR - This message should be saved in the table in audit schema")
26+
27+
log.WaitAndEnd()
28+
}

config/server.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package config
2+
3+
import (
4+
"os"
5+
"regexp"
6+
"strings"
7+
)
8+
9+
var (
10+
DbSchemaNameWithDot string
11+
DbSchemaName string
12+
DbTableName string
13+
)
14+
15+
func init() {
16+
DbSchemaName = ""
17+
DbSchemaNameWithDot = ""
18+
envSchemaName := os.Getenv("DB_SCHEMA_NAME")
19+
if envSchemaName != "" {
20+
SetDbSchemaName(envSchemaName)
21+
}
22+
23+
DbTableName = "app_log"
24+
envTableName := os.Getenv("DB_TABLE_NAME")
25+
if envTableName != "" {
26+
SetDbTableName(envTableName)
27+
}
28+
}
29+
30+
func SetDbTableName(tableName string) {
31+
if strings.TrimSpace(tableName) == "" {
32+
// If the value was empty, then set to default
33+
DbTableName = "app_log"
34+
return
35+
}
36+
37+
// Match alphanumerics and underscores
38+
pattern := `^[a-zA-Z0-9_]+$`
39+
40+
re, err := regexp.Compile(pattern)
41+
42+
if err != nil {
43+
return
44+
}
45+
46+
// Match the input string against the regular expression
47+
if re.MatchString(tableName) {
48+
// Input is alphanumeric with dashes and underscores
49+
DbTableName = tableName
50+
}
51+
}
52+
53+
func SetDbSchemaName(schemaName string) {
54+
if strings.TrimSpace(schemaName) == "" {
55+
DbSchemaName = ""
56+
DbSchemaNameWithDot = ""
57+
return
58+
}
59+
60+
// Match alphanumerics and dashes and underscores
61+
pattern := `^[a-zA-Z0-9_-]+$`
62+
63+
re, err := regexp.Compile(pattern)
64+
65+
if err != nil {
66+
return
67+
}
68+
69+
// Match the input string against the regular expression
70+
if re.MatchString(schemaName) {
71+
// Input is alphanumeric with dashes and underscores
72+
DbSchemaName = schemaName
73+
DbSchemaNameWithDot = schemaName + "."
74+
}
75+
}

models/barklog.go

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package models
33
import (
44
"context"
55
"fmt"
6+
7+
"github.com/techrail/bark/config"
68
"strings"
79
"time"
810

@@ -76,8 +78,8 @@ func NewBarkLogDao() *BarkLogDao {
7678

7779
// Insert inserts a Bark log in the database
7880
func (bld *BarkLogDao) Insert(l BarkLog) error {
79-
query := `
80-
INSERT INTO app_log (
81+
query := fmt.Sprintf(`
82+
INSERT INTO %v%v (
8183
log_time, log_level, service_name,
8284
service_instance_name, code, msg,
8385
more_data
@@ -86,7 +88,11 @@ func (bld *BarkLogDao) Insert(l BarkLog) error {
8688
$1, $2, $3,
8789
$4, $5, $6,
8890
$7
89-
)`
91+
)`, config.DbSchemaNameWithDot, config.DbTableName)
92+
93+
// fmt.Println("------------------")
94+
// fmt.Println(query)
95+
// fmt.Println("------------------")
9096

9197
_, err := resources.BarkDb.Client.Exec(context.Background(), query, l.LogTime, l.LogLevel, l.ServiceName,
9298
l.ServiceInstanceName, l.Code, l.Message,
@@ -121,11 +127,18 @@ func (bld *BarkLogDao) InsertBatch(l []BarkLog) error {
121127
batchOfBarkLog = append(batchOfBarkLog, batchElement)
122128
}
123129

124-
_, err := resources.BarkDb.Client.CopyFrom(context.Background(), pgx.Identifier{"app_log"},
125-
[]string{"log_time", "log_level", "service_name", "service_instance_name", "code", "msg", "more_data"}, pgx.CopyFromRows(batchOfBarkLog))
126-
127-
if err != nil {
128-
return fmt.Errorf("E#1KSPLS - error while inserting batch: %w", err)
130+
if config.DbSchemaName == "" {
131+
_, err := resources.BarkDb.Client.CopyFrom(context.Background(), pgx.Identifier{config.DbTableName},
132+
[]string{"log_time", "log_level", "service_name", "service_instance_name", "code", "msg", "more_data"}, pgx.CopyFromRows(batchOfBarkLog))
133+
if err != nil {
134+
return fmt.Errorf("E#1KSPLS - error while inserting batch: %w", err)
135+
}
136+
} else {
137+
_, err := resources.BarkDb.Client.CopyFrom(context.Background(), pgx.Identifier{config.DbSchemaName, config.DbTableName},
138+
[]string{"log_time", "log_level", "service_name", "service_instance_name", "code", "msg", "more_data"}, pgx.CopyFromRows(batchOfBarkLog))
139+
if err != nil {
140+
return fmt.Errorf("E#20JFGC - error while inserting batch: %w", err)
141+
}
129142
}
130143

131144
return nil

0 commit comments

Comments
 (0)