Commit 1680d073 authored by Rafael's avatar Rafael

feature: logs for resolvers

parent dd1be68b
Pipeline #21078422 passed with stage
in 2 minutes and 36 seconds
......@@ -8,4 +8,6 @@ COPY . .
RUN go get -d -v ./...
RUN go install -v ./...
VOLUME [ "/var/log/graphql" ]
CMD ["/usr/local/go/bin/go", "run", "main/main.go", "main/server.go", "main/relay.go", "main/mail.go"]
This diff is collapsed.
package generic
import (
"fmt"
log "github.com/sirupsen/logrus"
"gitlab.com/shereland/graphql/database"
"gitlab.com/shereland/graphql/logging"
)
type user struct {
......@@ -15,6 +15,11 @@ type user struct {
// FriendshipNotifications get friendship requests
func FriendshipNotifications(userid string) []map[string]string {
methodLog := logging.Log.WithFields(log.Fields{
"userid": userid,
"method": "FriendshipNotifications",
})
notificationsArray := make([]map[string]string, 0, 2)
db := database.DBCon
......@@ -30,15 +35,18 @@ func FriendshipNotifications(userid string) []map[string]string {
rows, err := db.Query(query, userid)
if err != nil {
fmt.Println(err)
methodLog.WithFields(log.Fields{
"err": err,
}).Error("Error in FriendshipNotifications")
}
defer rows.Close()
for rows.Next() {
var u user
if err := rows.Scan(&u.id, &u.slug, &u.firstName, &u.lastName); err != nil {
fmt.Println("Error on popular posts - err := rows.Scan")
fmt.Println(err)
methodLog.WithFields(log.Fields{
"err": err,
}).Error("Error in FriendshipNotifications")
}
user := make(map[string]string)
user["slug"] = u.slug
......@@ -46,7 +54,9 @@ func FriendshipNotifications(userid string) []map[string]string {
notificationsArray = append(notificationsArray, user)
}
if rows.Err() != nil {
fmt.Println("Error on popular posts - rows.Err() != nil")
methodLog.WithFields(log.Fields{
"err": err,
}).Error("Error in FriendshipNotifications")
}
return notificationsArray
......
package generic
import (
"fmt"
"net/url"
"os"
"time"
log "github.com/sirupsen/logrus"
"github.com/ChimeraCoder/anaconda"
"github.com/go-redis/redis"
"gitlab.com/shereland/graphql/database"
"gitlab.com/shereland/graphql/logging"
)
// GetLatestTweets Look for tweets at Redis DB
......@@ -25,15 +27,24 @@ func GetLatestTweets() []string {
}
func getFromRedis(client *redis.Client) []string {
methodLog := logging.Log.WithFields(log.Fields{
"method": "getFromRedis",
})
tweetsArray := make([]string, 0, 2)
tweet1, err1 := client.Get("latest_tweet1").Result()
tweet2, err2 := client.Get("latest_tweet2").Result()
if err1 != nil || err2 != nil {
fmt.Println(err1)
fmt.Println(err2)
methodLog.WithFields(log.Fields{
"err1": err1,
"err2": err2,
}).Error("getFromRedis")
return nil
}
if tweet1 == "" || tweet2 == "" {
methodLog.WithFields(log.Fields{
"err": "tweet1 and tweet2 are empty",
}).Warn("getFromRedis")
return nil
}
tweetsArray = append(tweetsArray, tweet1)
......
package logging
import (
log "github.com/sirupsen/logrus"
)
var (
Log *log.Entry
)
......@@ -6,19 +6,26 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"log"
"io/ioutil"
"net/http"
"os"
log "github.com/sirupsen/logrus"
"github.com/go-redis/redis"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"gitlab.com/shereland/graphql/database"
"gitlab.com/shereland/graphql/logging"
"gitlab.com/shereland/graphql/metrics"
)
func main() {
var err error
/*
* Initialize databases
*/
dsn := os.Getenv("MYSQL_USER") + ":" + os.Getenv("MYSQL_PASSWORD") + "@(monolithic_db:3306)/shereland?parseTime=true"
database.DBCon, err = sql.Open("mysql", dsn)
if err != nil {
......@@ -31,6 +38,34 @@ func main() {
})
prometheus.MustRegister(metrics.ResolverDurations)
/*
* Log everything to /var/log/graphql/all.log
*/
log.SetFormatter(&log.JSONFormatter{})
file, err := os.OpenFile("/var/log/graphql/all.log", os.O_CREATE|os.O_WRONLY, 0666)
if err == nil {
log.SetOutput(file)
} else {
log.Info("Failed to log to file, using default stderr")
}
log.SetOutput(file)
log.SetLevel(log.DebugLevel)
var hostname string
b, err := ioutil.ReadFile("/etc/hostname")
if err != nil {
hostname = "no hostname"
} else {
hostname = string(b)
hostname = hostname[0 : len(hostname)-2]
}
logging.Log = log.WithFields(log.Fields{
"hostname": hostname,
"service": "graphql",
})
/*
* Handle requests
*/
http.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write(page)
}))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment