Skip to content

Commit

Permalink
Add Slack logs, enhance some log messages
Browse files Browse the repository at this point in the history
  • Loading branch information
llemeurfr committed Dec 29, 2023
1 parent 04a40b1 commit 6e0634e
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 44 deletions.
24 changes: 17 additions & 7 deletions lcpserver/api/license.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,8 @@ func GetLicense(w http.ResponseWriter, r *http.Request, s Server) {
// get the license id from the request URL
licenseID := vars["license_id"]

logging.Print("Get License with id " + licenseID)
// add a log
logging.Print("Get the License " + licenseID)

// initialize the license from the info stored in the db.
var licOut license.License
Expand Down Expand Up @@ -377,7 +378,8 @@ func GenerateLicense(w http.ResponseWriter, r *http.Request, s Server) {
return
}

log.Println("New License:", lic.ID, "Content:", contentID, "User:", lic.User.ID)
// add a log
logging.Print("Generate a License " + lic.ID + " for Content " + contentID + " and User " + lic.User.ID)

// set http headers
w.Header().Add("Content-Type", api.ContentType_LCP_JSON)
Expand All @@ -402,7 +404,8 @@ func GetLicensedPublication(w http.ResponseWriter, r *http.Request, s Server) {
vars := mux.Vars(r)
licenseID := vars["license_id"]

log.Println("Get a Licensed publication for license id", licenseID)
// add a log
logging.Print("Get a Licensed publication for License " + licenseID)

// get the input body
var licIn license.License
Expand Down Expand Up @@ -472,7 +475,7 @@ func GenerateLicensedPublication(w http.ResponseWriter, r *http.Request, s Serve
vars := mux.Vars(r)
contentID := vars["content_id"]

log.Println("Generate a Licensed publication for content id", contentID)
logging.Print("Generate a Licensed publication for Content " + contentID)

// get the input body
var lic license.License
Expand Down Expand Up @@ -553,7 +556,8 @@ func UpdateLicense(w http.ResponseWriter, r *http.Request, s Server) {
// get the license id from the request URL
licenseID := vars["license_id"]

log.Println("Update License with id", licenseID)
// add a log
logging.Print("Update the License " + licenseID)

var licIn license.License
err := DecodeJSONLicense(r, &licIn)
Expand Down Expand Up @@ -645,7 +649,10 @@ func ListLicenses(w http.ResponseWriter, r *http.Request, s Server) {
return
}
licenses := make([]license.LicenseReport, 0)
//log.Println("ListAll(" + strconv.Itoa(int(per_page)) + "," + strconv.Itoa(int(page)) + ")")

// add a log
logging.Print("List Licenses (page " + strconv.Itoa(int(page)) + ", count " + strconv.Itoa(int(perPage)) + ")")

fn := s.Licenses().ListAll(int(perPage), int(page))
for it, err := fn(); err == nil; it, err = fn() {
licenses = append(licenses, it)
Expand Down Expand Up @@ -717,7 +724,10 @@ func ListLicensesForContent(w http.ResponseWriter, r *http.Request, s Server) {
return
}
licenses := make([]license.LicenseReport, 0)
//log.Println("List(" + contentId + "," + strconv.Itoa(int(per_page)) + "," + strconv.Itoa(int(page)) + ")")

// add a log
logging.Print("List Licenses for publication " + contentID + " (page " + strconv.Itoa(int(page)) + ", count " + strconv.Itoa(int(perPage)) + ")")

fn := s.Licenses().ListByContentID(contentID, int(perPage), int(page))
for it, err := fn(); err == nil; it, err = fn() {
licenses = append(licenses, it)
Expand Down
18 changes: 17 additions & 1 deletion lcpserver/api/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ import (
"net/http"
"net/url"
"os"
"strconv"

"github.com/gorilla/mux"

"github.com/readium/readium-lcp-server/api"
"github.com/readium/readium-lcp-server/index"
"github.com/readium/readium-lcp-server/license"
"github.com/readium/readium-lcp-server/logging"
"github.com/readium/readium-lcp-server/pack"
"github.com/readium/readium-lcp-server/problem"
"github.com/readium/readium-lcp-server/storage"
Expand Down Expand Up @@ -129,7 +131,10 @@ func AddContent(w http.ResponseWriter, r *http.Request, s Server) {
return
}

// if the encrypted publication has not been stored yet
// add a log
logging.Print("Add publication " + contentID)

// if the encrypted publication has not been already stored by lcpencrypt
if encrypted.StorageMode == Storage_none {

// open the encrypted file, use its full path
Expand Down Expand Up @@ -192,6 +197,9 @@ func ListContents(w http.ResponseWriter, r *http.Request, s Server) {
contents = append(contents, it)
}

// add a log
logging.Print("List publications, total " + strconv.Itoa(len(contents)))

w.Header().Set("Content-Type", api.ContentType_JSON)
enc := json.NewEncoder(w)
err := enc.Encode(contents)
Expand All @@ -209,6 +217,10 @@ func GetContent(w http.ResponseWriter, r *http.Request, s Server) {
// get the content id from the calling url
vars := mux.Vars(r)
contentID := vars["content_id"]

// add a log
logging.Print("Get publication " + contentID)

content, err := s.Index().Get(contentID)
if err != nil { //item probably not found
if err == index.ErrNotFound {
Expand Down Expand Up @@ -256,6 +268,10 @@ func DeleteContent(w http.ResponseWriter, r *http.Request, s Server) {
// get the content id from the calling url
vars := mux.Vars(r)
contentID := vars["content_id"]

// add a log
logging.Print("Delete publication " + contentID)

err := s.Index().Delete(contentID)
if err != nil { //item probably not found
if err == index.ErrNotFound {
Expand Down
22 changes: 18 additions & 4 deletions logging/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,24 @@
package logging

import (
"errors"
"log"
"os"
"time"

"github.com/readium/readium-lcp-server/config"
"github.com/slack-go/slack"
)

// LogFile is the Logger file
var (
LogFile *log.Logger
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)
Expand All @@ -31,9 +35,13 @@ func Init(logging config.Logging) error {
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
}

Expand All @@ -49,4 +57,10 @@ func Print(message string) {
}

// 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)
}
}
}
41 changes: 9 additions & 32 deletions problem/problem.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,6 @@
// Copyright (c) 2016 Readium Foundation
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation and/or
// other materials provided with the distribution.
// 3. Neither the name of the organization nor the names of its contributors may be
// used to endorse or promote products derived from this software without specific
// prior written permission
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// Copyright 2020 Readium Foundation. All rights reserved.
// 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 problem

Expand Down Expand Up @@ -90,12 +69,12 @@ func Error(w http.ResponseWriter, r *http.Request, problem Problem, status int)
}
fmt.Fprintln(w, string(jsonError))

// debug only
//PrintStack()

msg := fmt.Sprintf("Error: %s (%d) = %s", problem.Title, problem.Status, problem.Detail)
// log the error persistently
msg := fmt.Sprintf("Error: %s (%d). %s", problem.Title, problem.Status, problem.Detail)
logging.Print(msg)

// debug only
//PrintStack()
}

func PrintStack() {
Expand All @@ -116,9 +95,7 @@ func PrintStack() {
log.Print("####################")
}

// NotFoundHandler handles 404 API errors
func NotFoundHandler(w http.ResponseWriter, r *http.Request) {
var problem Problem
problem.Type = LICENSE_NOT_FOUND
problem.Title = "Failed to find the license ID"
Error(w, r, problem, http.StatusNotFound)
Error(w, r, Problem{Detail: r.URL.String()}, http.StatusNotFound)
}

0 comments on commit 6e0634e

Please sign in to comment.