-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathlogging.go
66 lines (56 loc) · 1.63 KB
/
logging.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright 2023 European Digital Reading Lab. All rights reserved.
// Licensed to the Readium Foundation under one or more contributor license agreements.
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file exposed on Github (readium) in the project repository.
package logging
import (
"errors"
"log"
"os"
"time"
"github.com/readium/readium-lcp-server/config"
"github.com/slack-go/slack"
)
var (
LogFile *log.Logger
SlackApi *slack.Client
SlackChannelID string
)
// Init inits the log file and opens it
func Init(logging config.Logging) error {
//logPath string, cm bool
if logging.Directory != "" {
log.Println("Open log file as " + logging.Directory)
file, err := os.OpenFile(logging.Directory, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0666)
if err != nil {
return err
}
LogFile = log.New(file, "", log.LUTC)
}
if logging.SlackToken != "" && logging.SlackChannelID != "" {
log.Println("Init Slack connection")
SlackApi = slack.New(logging.SlackToken)
if SlackApi == nil {
return errors.New("error creating a Slack connector")
}
SlackChannelID = logging.SlackChannelID
}
return nil
}
// Print writes a message to the log file / Slack
func Print(message string) {
// log on stdout
log.Print(message)
// log on a file
if LogFile != nil {
currentTime := time.Now().UTC().Format(time.RFC3339)
LogFile.Println(currentTime + "\t" + message)
}
// log on Slack
if SlackApi != nil {
_, _, err := SlackApi.PostMessage(SlackChannelID, slack.MsgOptionText(message, false))
if err != nil {
log.Printf("Error sending Slack msg, %v", err)
}
}
}