mirror of
https://github.com/Dvorinka/excalidraw-full.git
synced 2026-06-03 22:02:57 +00:00
Add structured logging to documentStore methods
To troubleshoot reported issues, we add more debugging outputs in the logging. This includes detailed, structured logs in the Create and FindID methods to provide better traceability and debuggability. These changes help diagnose issues by providing detailed context in the logs, which should improve the ability to identify and resolve problems. See also: #9
This commit is contained in:
@@ -10,6 +10,7 @@ import (
|
||||
"path/filepath"
|
||||
|
||||
"github.com/oklog/ulid/v2"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type documentStore struct {
|
||||
@@ -26,12 +27,16 @@ func NewDocumentStore(basePath string) core.DocumentStore {
|
||||
|
||||
func (s *documentStore) FindID(ctx context.Context, id string) (*core.Document, error) {
|
||||
filePath := filepath.Join(s.basePath, id)
|
||||
log := logrus.WithField("document_id", id)
|
||||
|
||||
log.WithField("file_path", filePath).Info("Retrieving document by ID")
|
||||
data, err := os.ReadFile(filePath)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
log.WithField("error", "document not found").Warn("Document with specified ID not found")
|
||||
return nil, fmt.Errorf("document with id %s not found", id)
|
||||
}
|
||||
log.WithField("error", err).Error("Failed to retrieve document")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -39,16 +44,24 @@ func (s *documentStore) FindID(ctx context.Context, id string) (*core.Document,
|
||||
Data: *bytes.NewBuffer(data),
|
||||
}
|
||||
|
||||
log.Info("Document retrieved successfully")
|
||||
return &document, nil
|
||||
}
|
||||
|
||||
func (s *documentStore) Create(ctx context.Context, document *core.Document) (string, error) {
|
||||
id := ulid.Make().String()
|
||||
filePath := filepath.Join(s.basePath, id)
|
||||
log := logrus.WithFields(logrus.Fields{
|
||||
"document_id": id,
|
||||
"file_path": filePath,
|
||||
})
|
||||
log.Info("Creating new document")
|
||||
|
||||
if err := os.WriteFile(filePath, document.Data.Bytes(), 0644); err != nil {
|
||||
log.WithField("error", err).Error("Failed to create document")
|
||||
return "", err
|
||||
}
|
||||
|
||||
log.Info("Document created successfully")
|
||||
return id, nil
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"fmt"
|
||||
|
||||
"github.com/oklog/ulid/v2"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
var savedDocuments = make(map[string]core.Document)
|
||||
@@ -18,14 +19,23 @@ func NewDocumentStore() core.DocumentStore {
|
||||
}
|
||||
|
||||
func (s *documentStore) FindID(ctx context.Context, id string) (*core.Document, error) {
|
||||
log := logrus.WithField("document_id", id)
|
||||
if val, ok := savedDocuments[id]; ok {
|
||||
log.Info("Document retrieved successfully")
|
||||
return &val, nil
|
||||
}
|
||||
log.WithField("error", "document not found").Warn("Document with specified ID not found")
|
||||
return nil, fmt.Errorf("document with id %s not found", id)
|
||||
}
|
||||
|
||||
func (s *documentStore) Create(ctx context.Context, document *core.Document) (string, error) {
|
||||
id := ulid.Make().String()
|
||||
savedDocuments[id] = *document
|
||||
log := logrus.WithFields(logrus.Fields{
|
||||
"document_id": id,
|
||||
"data_length": len(document.Data.Bytes()),
|
||||
})
|
||||
log.Info("Document created successfully")
|
||||
|
||||
return id, nil
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
"github.com/oklog/ulid/v2"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
var savedDocuments = make(map[string]core.Document)
|
||||
@@ -35,26 +36,38 @@ func NewDocumentStore(dataSourceName string) core.DocumentStore {
|
||||
}
|
||||
|
||||
func (s *documentStore) FindID(ctx context.Context, id string) (*core.Document, error) {
|
||||
log := logrus.WithField("document_id", id)
|
||||
log.Debug("Retrieving document by ID")
|
||||
var data []byte
|
||||
err := s.db.QueryRowContext(ctx, "SELECT data FROM documents WHERE id = ?", id).Scan(&data)
|
||||
if err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
log.WithField("error", "document not found").Warn("Document with specified ID not found")
|
||||
return nil, fmt.Errorf("document with id %s not found", id)
|
||||
}
|
||||
log.WithField("error", err).Error("Failed to retrieve document")
|
||||
return nil, err
|
||||
}
|
||||
document := core.Document{
|
||||
Data: *bytes.NewBuffer(data),
|
||||
}
|
||||
log.Info("Document retrieved successfully")
|
||||
return &document, nil
|
||||
}
|
||||
|
||||
func (s *documentStore) Create(ctx context.Context, document *core.Document) (string, error) {
|
||||
id := ulid.Make().String()
|
||||
data := document.Data.Bytes()
|
||||
log := logrus.WithFields(logrus.Fields{
|
||||
"document_id": id,
|
||||
"data_length": len(data),
|
||||
})
|
||||
|
||||
_, err := s.db.ExecContext(ctx, "INSERT INTO documents (id, data) VALUES (?, ?)", id, data)
|
||||
if err != nil {
|
||||
log.WithField("error", err).Error("Failed to create document")
|
||||
return "", err
|
||||
}
|
||||
log.Info("Document created successfully")
|
||||
return id, nil
|
||||
}
|
||||
|
||||
@@ -7,24 +7,35 @@ import (
|
||||
"excalidraw-complete/stores/memory"
|
||||
"excalidraw-complete/stores/sqlite"
|
||||
"os"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func GetStore() core.DocumentStore {
|
||||
storageType := os.Getenv("STORAGE_TYPE")
|
||||
var store core.DocumentStore
|
||||
|
||||
storageField := logrus.Fields{
|
||||
"storageType": storageType,
|
||||
}
|
||||
|
||||
switch storageType {
|
||||
case "filesystem":
|
||||
basePath := os.Getenv("LOCAL_STORAGE_PATH")
|
||||
storageField["basePath"] = basePath
|
||||
store = filesystem.NewDocumentStore(basePath)
|
||||
case "sqlite":
|
||||
dataSourceName := os.Getenv("DATA_SOURCE_NAME")
|
||||
storageField["dataSourceName"] = dataSourceName
|
||||
store = sqlite.NewDocumentStore(dataSourceName)
|
||||
case "s3":
|
||||
bucketName := os.Getenv("S3_BUCKET_NAME")
|
||||
storageField["bucketName"] = bucketName
|
||||
store = aws.NewDocumentStore(bucketName)
|
||||
default:
|
||||
store = memory.NewDocumentStore()
|
||||
storageField["storageType"] = "in-memory"
|
||||
}
|
||||
logrus.WithFields(storageField).Info("Use storage")
|
||||
return store
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user