From 857f1ef06c79d6c894cc272a605b75b78ebe261b Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Tue, 18 Jul 2023 14:09:28 +0100 Subject: [PATCH] Reimplement logging --- src/dbAccess/client.go | 7 +++--- src/go.mod | 5 ++++ src/go.sum | 32 ++++++++++++++++++++++++++ src/helpers/config.go | 4 +++- src/helpers/log.go | 25 -------------------- src/log/log.go | 21 +++++++++++++++++ src/main.go | 20 +++++----------- src/messaging/client.go | 51 +++++++++++++++++++++++++++++++++++++++++ src/messaging/listen.go | 26 +++++++++++++++++++++ 9 files changed, 148 insertions(+), 43 deletions(-) delete mode 100644 src/helpers/log.go create mode 100644 src/log/log.go create mode 100644 src/messaging/listen.go diff --git a/src/dbAccess/client.go b/src/dbAccess/client.go index 365f7fa..a0ea125 100644 --- a/src/dbAccess/client.go +++ b/src/dbAccess/client.go @@ -1,7 +1,7 @@ package dbAccess import ( - "git.fjla.uk/owlboard/mq-client/helpers" + "git.fjla.uk/owlboard/mq-client/log" "context" "fmt" @@ -17,6 +17,7 @@ import ( var dbUri string = getDbUri() func getDbUri() string { + log.Msg.Debug("Fetching DB Access details") var dbHost string = os.Getenv("OWL_DB_HOST") if dbHost == "" { dbHost = "localhost" @@ -47,9 +48,9 @@ func initDataAccess() *mongo.Client { client, err := mongo.Connect(ctx, options.Client().ApplyURI(dbUri)) if err != nil { fmt.Println(err) - helpers.Log("Error connecting to database", 4) + log.Msg.Fatal("Error connecting to database: " + err.Error()) } else { - helpers.Log("Connected to Database", 1) + log.Msg.Info("Connected to Database") } return client } diff --git a/src/go.mod b/src/go.mod index 809d6aa..4135091 100644 --- a/src/go.mod +++ b/src/go.mod @@ -6,6 +6,8 @@ require go.mongodb.org/mongo-driver v1.12.0 require ( git.fjla.uk/owlboard/go-types v0.0.0-20230717113641-a1216e6e5106 // indirect + github.com/go-stomp/stomp v2.1.4+incompatible // indirect + github.com/go-stomp/stomp/v3 v3.0.5 // indirect github.com/golang/snappy v0.0.1 // indirect github.com/klauspost/compress v1.13.6 // indirect github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe // indirect @@ -13,6 +15,9 @@ require ( github.com/xdg-go/scram v1.1.2 // indirect github.com/xdg-go/stringprep v1.0.4 // indirect github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect + go.uber.org/atomic v1.7.0 // indirect + go.uber.org/multierr v1.6.0 // indirect + go.uber.org/zap v1.24.0 // indirect golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d // indirect golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 // indirect golang.org/x/text v0.7.0 // indirect diff --git a/src/go.sum b/src/go.sum index a73a690..2b636a8 100644 --- a/src/go.sum +++ b/src/go.sum @@ -2,16 +2,28 @@ git.fjla.uk/owlboard/go-types v0.0.0-20230717112031-d30d0275718d h1:wpU+R71igyQ8 git.fjla.uk/owlboard/go-types v0.0.0-20230717112031-d30d0275718d/go.mod h1:kG+BX9UF+yJaAVnln/QSKlTdrtKRRReezMeSk1ZLMzY= git.fjla.uk/owlboard/go-types v0.0.0-20230717113641-a1216e6e5106 h1:1sPFYr4/gEdWGWJhB4/Y7jJYemBSZBXcWtBm7XiZqZk= git.fjla.uk/owlboard/go-types v0.0.0-20230717113641-a1216e6e5106/go.mod h1:kG+BX9UF+yJaAVnln/QSKlTdrtKRRReezMeSk1ZLMzY= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/go-stomp/stomp v2.1.4+incompatible h1:D3SheUVDOz9RsjVWkoh/1iCOwD0qWjyeTZMUZ0EXg2Y= +github.com/go-stomp/stomp v2.1.4+incompatible/go.mod h1:VqCtqNZv1226A1/79yh+rMiFUcfY3R109np+7ke4n0c= +github.com/go-stomp/stomp/v3 v3.0.5 h1:yOORvXLqSu0qF4loJjfWrcVE1o0+9cFudclcP0an36Y= +github.com/go-stomp/stomp/v3 v3.0.5/go.mod h1:ztzZej6T2W4Y6FlD+Tb5n7HQP3/O5UNQiuC169pIp10= +github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs= github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4= github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM= github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/klauspost/compress v1.13.6 h1:P76CopJELS0TiO2mebmnzgWaajssP/EszplttgQxcgc= github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk= +github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe h1:iruDEfMl2E6fbMZ9s0scYfZQ84/6SPL6zC8ACM2oIL0= github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c= github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI= github.com/xdg-go/scram v1.1.2 h1:FHX5I5B4i4hKRVRBCFRxq1iQRej7WO3hhBuJf+UUySY= @@ -20,24 +32,39 @@ github.com/xdg-go/stringprep v1.0.4 h1:XLI/Ng3O1Atzq0oBs3TWm+5ZVgkq2aqdlvP9JtoZ6 github.com/xdg-go/stringprep v1.0.4/go.mod h1:mPGuuIYwz7CmR2bT9j4GbQqutWS1zV24gijq1dTyGkM= github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d h1:splanxYIlg+5LfHAM6xpdFEAYOk8iySO56hMFq6uLyA= github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d/go.mod h1:rHwXgn7JulP+udvsHwJoVG1YGAP6VLg4y9I5dyZdqmA= +github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= go.mongodb.org/mongo-driver v1.12.0 h1:aPx33jmn/rQuJXPQLZQ8NtfPQG8CaqgLThFtqRb0PiE= go.mongodb.org/mongo-driver v1.12.0/go.mod h1:AZkxhPnFJUoH7kZlFkVKucV20K387miPfm7oimrSmK0= +go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw= +go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= +go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4= +go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU= +go.uber.org/zap v1.24.0 h1:FiJd5l1UOLj0wCgbSE0rwwXHzEdAZS6hiiSnxJN/D60= +go.uber.org/zap v1.24.0/go.mod h1:2kMP+WWQ8aoFoedH3T2sq6iJ2yDWpHbP0f6MQbS9Gkg= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d h1:sK3txAijHtOK88l68nt020reeT1ZdKLIYetKl95FzVY= golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= +golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 h1:uVc8UZUe6tr40fFVnUP5Oj+veunVezqYl9z7DYw9xzw= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= @@ -52,7 +79,12 @@ golang.org/x/text v0.7.0 h1:4BRB4x83lYWy72KwLD/qYDuTu7q9PjSagHvijDw7cLo= golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= diff --git a/src/helpers/config.go b/src/helpers/config.go index 792eee3..3f2aab1 100644 --- a/src/helpers/config.go +++ b/src/helpers/config.go @@ -3,6 +3,8 @@ package helpers import ( "fmt" "os" + + "git.fjla.uk/owlboard/mq-client/log" ) // Version Constants @@ -21,6 +23,6 @@ func getRuntime() string { if runtimeEnv == "" { runtimeEnv = "unknown" } - Out(fmt.Sprintf("Runtime mode: %s", runtimeEnv), 1) + log.Msg.Info(fmt.Sprintf("Runtime mode: %s", runtimeEnv)) return runtimeEnv } diff --git a/src/helpers/log.go b/src/helpers/log.go deleted file mode 100644 index 3dc2fe4..0000000 --- a/src/helpers/log.go +++ /dev/null @@ -1,25 +0,0 @@ -package helpers - -import ( - "fmt" - "time" - "os" -) - -var levels = [5] string {"DBUG", "INFO", "WARN", "EROR", "FATL"} - -func Log(message string, level int) { - if runtime == "production" && level < 2 { - return - } - Out(message, level); - if level == 4 { - os.Exit(1); - } -} - -func Out(message string, level int) { - var date = time.Now() - var now string = date.Format("2006-01-02 15:04:05.0000") - fmt.Println(now, "::", levels[level], "::", message) -} \ No newline at end of file diff --git a/src/log/log.go b/src/log/log.go new file mode 100644 index 0000000..9814fd8 --- /dev/null +++ b/src/log/log.go @@ -0,0 +1,21 @@ +package log + +import ( + "go.uber.org/zap" + "go.uber.org/zap/zapcore" +) + +var Msg *zap.Logger + +func init() { + var err error + + // Create a custom configuration with a human-readable "Console" encoder + config := zap.NewDevelopmentConfig() + config.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder // Adds color to log levels + + Msg, err = config.Build() + if err != nil { + panic("Failed to initialize logger: " + err.Error()) + } +} diff --git a/src/main.go b/src/main.go index 2fd97da..02c5fbe 100644 --- a/src/main.go +++ b/src/main.go @@ -1,22 +1,14 @@ package main import ( - "git.fjla.uk/owlboard/mq-client/helpers" "git.fjla.uk/owlboard/mq-client/dbAccess" - "git.fjla.uk/owlboard/go-types/pkg/database" - "fmt" + "git.fjla.uk/owlboard/mq-client/helpers" + "git.fjla.uk/owlboard/mq-client/log" + "git.fjla.uk/owlboard/mq-client/messaging" ) func main() { - helpers.Out("Initialised OwlBoard MQ Client " + helpers.Version, 0) - var test database.Stop = database.Stop{ - PublicDeparture: nil, - WttDeparture: nil, - PublicArrival: nil, - WttArrival: nil, - IsPublic: false, - Tiploc: "Testing", - } - fmt.Println(test) + log.Msg.Info("Initialised OwlBoard MQ Client " + helpers.Version) dbAccess.PrintFromDbPackage() -} \ No newline at end of file + messaging.Listen() +} diff --git a/src/messaging/client.go b/src/messaging/client.go index 5ef9519..f042028 100644 --- a/src/messaging/client.go +++ b/src/messaging/client.go @@ -1,3 +1,54 @@ package messaging +import ( + "os" + + "git.fjla.uk/owlboard/mq-client/log" + "go.uber.org/zap" + + "github.com/go-stomp/stomp/v3" +) + +type nrodCredential struct { + user string + pass string +} + +func getCredentials() nrodCredential { + var nrod_user string = os.Getenv("OWL_LDB_CORPUSUSER") + var nrod_pass string = os.Getenv("OWL_LDB_CORPUSPASS") + if nrod_user == "" || nrod_pass == "" { + log.Msg.Fatal("No NROD Credentials provided") + } + return nrodCredential{ + user: nrod_user, + pass: nrod_pass, + } +} + +var client = dial() + +func dial() *stomp.Conn { + var credentials nrodCredential = getCredentials() + conn, err := stomp.Dial("tcp", "publicdatafeeds.networkrail.co.uk:61618", + stomp.ConnOpt.Login(credentials.user, credentials.pass), + stomp.ConnOpt.HeartBeat(15000, 15000), + stomp.ConnOpt.Header("client-id", credentials.user), + ) + if err != nil { + log.Msg.Fatal("Unable to connect to STOMP Client", zap.String("err", err.Error())) + } + defer disconnect(conn) + return conn +} + +func disconnect(conn *stomp.Conn) { + if conn != nil { + err := conn.Disconnect() + if err != nil { + conn.MustDisconnect() + } + } +} + // Register against the MQ Server and log each message for testing purposes diff --git a/src/messaging/listen.go b/src/messaging/listen.go new file mode 100644 index 0000000..9432d7a --- /dev/null +++ b/src/messaging/listen.go @@ -0,0 +1,26 @@ +package messaging + +import ( + "fmt" + + "git.fjla.uk/owlboard/mq-client/log" + + "github.com/go-stomp/stomp/v3" +) + +func Listen() { + sub, err := client.Subscribe("/topic/VSTP_ALL", stomp.AckAuto) + if err != nil { + log.Msg.Fatal("Unable to start subscription: " + err.Error()) + } + + msg := <-sub.C + if msg.Err != nil { + fmt.Println(msg.Err) + } + handle(msg) +} + +func handle(msg *stomp.Message) { + fmt.Println(msg) +}