TP-45730 | creating a global logger (#263)

This commit is contained in:
Shashank Shekhar
2023-11-02 13:11:52 +05:30
committed by GitHub
parent bf1da192bd
commit a9ffffc31a
75 changed files with 767 additions and 831 deletions

View File

@@ -1,6 +1,7 @@
package postgres
import (
"houston/logger"
"os"
"time"
@@ -9,12 +10,9 @@ import (
"gorm.io/gorm"
)
type Client struct {
logger *zap.Logger
gormClient *gorm.DB
}
type Client struct{ gormClient *gorm.DB }
func NewGormClient(dsn string, connMaxIdleTime string, connMaxLifetime string, setMaxIdleConns int, setMaxOpenConns int, logger *zap.Logger) *gorm.DB {
func NewGormClient(dsn string, connMaxIdleTime string, connMaxLifetime string, setMaxIdleConns int, setMaxOpenConns int) *gorm.DB {
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
if err != nil {
logger.Error("database connection failed", zap.Error(err))

View File

@@ -3,6 +3,7 @@ package s3
import (
"context"
"fmt"
"houston/logger"
"os"
"path/filepath"
@@ -20,21 +21,20 @@ type S3Operations interface {
GetFileLink(bucketName, resourceName string) (string, error)
}
func NewS3Operations(logger *zap.Logger) S3Operations {
func NewS3Operations() S3Operations {
if s3Operations == nil {
s3Operations = newS3Client(logger)
s3Operations = newS3Client()
return s3Operations
}
return s3Operations
}
type client struct {
logger *zap.Logger
s3Client *s3.Client
presignClient *s3.PresignClient
}
func newS3Client(logger *zap.Logger) *client {
func newS3Client() *client {
cfg, err := config.LoadDefaultConfig(context.TODO(), config.WithRegion("ap-south-1"))
if err != nil {
logger.Error("s3 config load failed", zap.Error(err))
@@ -45,7 +45,6 @@ func newS3Client(logger *zap.Logger) *client {
preSignClient := s3.NewPresignClient(awsS3Client)
return &client{
logger: logger,
s3Client: awsS3Client,
presignClient: preSignClient,
}

View File

@@ -2,6 +2,7 @@ package slackbot
import (
"fmt"
"houston/logger"
"github.com/slack-go/slack"
"go.uber.org/zap"
@@ -14,7 +15,7 @@ func (c *Client) FindParticipants(channelId string) ([]string, error) {
}
channelInfo, _, err := c.socketModeClient.GetUsersInConversation(request)
if err != nil {
c.logger.Error("find participants failed", zap.String("channel_id", channelId), zap.Error(err))
logger.Error("find participants failed", zap.String("channel_id", channelId), zap.Error(err))
return nil, fmt.Errorf("fetch channel conversationInfo failed. err: %v", err)
}
@@ -29,34 +30,34 @@ func (c *Client) CreateChannel(channelName string) (string, error) {
channel, err := c.socketModeClient.CreateConversation(request)
if err != nil {
c.logger.Error("create slackbot channel failed", zap.String("channel_name", channelName), zap.Error(err))
logger.Error("create slackbot channel failed", zap.String("channel_name", channelName), zap.Error(err))
return "", err
}
c.logger.Info("created slackbot channel successfully", zap.String("channel_name", channelName), zap.String("channel_id", channel.ID))
logger.Info("created slackbot channel successfully", zap.String("channel_name", channelName), zap.String("channel_id", channel.ID))
return channel.ID, nil
}
func (c *Client) SetChannelTopic(channelId, topic string) (string, error) {
channel, err := c.socketModeClient.SetTopicOfConversation(channelId, topic)
if err != nil {
c.logger.Error("set topic on slack channel failed", zap.String("channel_id", channelId), zap.Error(err))
logger.Error("set topic on slack channel failed", zap.String("channel_id", channelId), zap.Error(err))
return "", err
}
c.logger.Info("set topic on slack channel successful", zap.String("channel_id", channelId), zap.String("channel_id", channel.ID))
logger.Info("set topic on slack channel successful", zap.String("channel_id", channelId), zap.String("channel_id", channel.ID))
return "", nil
}
func (c *Client) InviteUsersToConversation(channelId string, userId ...string) {
_, err := c.socketModeClient.InviteUsersToConversation(channelId, userId...)
if err != nil {
c.logger.Error("invite users to conversation failed",
logger.Error("invite users to conversation failed",
zap.String("channel_id", channelId), zap.Any("user_ids", userId), zap.Error(err))
return
}
c.logger.Info("successfully invite users to conversation", zap.String("channel_id", channelId), zap.Any("user_ids", userId))
logger.Info("successfully invite users to conversation", zap.String("channel_id", channelId), zap.Any("user_ids", userId))
}
func (c *Client) GetConversationInfo(channelId string) (*slack.Channel, error) {
@@ -64,7 +65,7 @@ func (c *Client) GetConversationInfo(channelId string) (*slack.Channel, error) {
ChannelID: channelId,
})
if err != nil {
c.logger.Info("failed while fetching conversation info", zap.Error(err))
logger.Info("failed while fetching conversation info", zap.Error(err))
return nil, err
}
return channel, nil

View File

@@ -2,17 +2,14 @@ package slackbot
import (
"github.com/slack-go/slack/socketmode"
"go.uber.org/zap"
)
type Client struct {
socketModeClient *socketmode.Client
logger *zap.Logger
}
func NewSlackClient(logger *zap.Logger, socketModeClient *socketmode.Client) *Client {
func NewSlackClient(socketModeClient *socketmode.Client) *Client {
return &Client{
socketModeClient: socketModeClient,
logger: logger,
}
}

View File

@@ -4,6 +4,7 @@ import (
"github.com/slack-go/slack"
"github.com/thoas/go-funk"
"go.uber.org/zap"
"houston/logger"
)
func (c *Client) GetUsersInfo(users ...string) (*[]slack.User, error) {
@@ -15,7 +16,7 @@ func (c *Client) GetUsersInfo(users ...string) (*[]slack.User, error) {
for usersList := range splittedUsersList {
usersInfoResponse, err := c.socketModeClient.GetUsersInfo(splittedUsersList[usersList]...)
if err != nil {
c.logger.Error("get users info failed", zap.Any("users", splittedUsersList[usersList]), zap.Error(err))
logger.Error("get users info failed", zap.Any("users", splittedUsersList[usersList]), zap.Error(err))
emptyArray := make([]slack.User, len(splittedUsersList))
return &emptyArray, nil
}
@@ -31,7 +32,7 @@ func (c *Client) GetUsersInConversation(channelId string) ([]string, error) {
ChannelID: channelId,
})
if err != nil {
c.logger.Error("error in getting users from conversations",
logger.Error("error in getting users from conversations",
zap.String("channelId", channelId), zap.Error(err))
return nil, err
}
@@ -41,7 +42,7 @@ func (c *Client) GetUsersInConversation(channelId string) ([]string, error) {
func (c *Client) GetUserByEmail(userEmail string) (*slack.User, error) {
user, err := c.socketModeClient.GetUserByEmail(userEmail)
if err != nil {
c.logger.Error("error in getting user by email", zap.String("email", userEmail), zap.Error(err))
logger.Error("error in getting user by email", zap.String("email", userEmail), zap.Error(err))
return nil, err
}
return user, err
@@ -50,7 +51,7 @@ func (c *Client) GetUserByEmail(userEmail string) (*slack.User, error) {
func (c *Client) GetUserEmailById(userId string) (string, error) {
users, err := c.GetUsersInfo(userId)
if err != nil {
c.logger.Error("error in getting user info by id", zap.String("id", userId), zap.Error(err))
logger.Error("error in getting user info by id", zap.String("id", userId), zap.Error(err))
return "Anonymous-" + userId, err
}
usersInfo := *users
@@ -62,7 +63,7 @@ func (c *Client) GetUserEmailsOrNameByIds(userIds ...string) (map[string]string,
users, err := c.GetUsersInfo(userIds...)
if err != nil {
c.logger.Error("error in getting user info by id", zap.Any("id", userIds), zap.Error(err))
logger.Error("error in getting user info by id", zap.Any("id", userIds), zap.Error(err))
return userIdAndIdentityMapping, err
}
usersInfo := *users