Ensure an id is included when serializing a log entry

This commit is contained in:
Dan Jones 2026-02-05 13:11:28 -06:00
commit 3b36055b1b
3 changed files with 16 additions and 1 deletions

View file

@ -5,12 +5,15 @@ import (
"bytes"
"encoding/json"
"errors"
"fmt"
"os"
"regexp"
"strings"
"sync"
"time"
"codeberg.org/danjones000/my-log/tools"
"github.com/google/uuid"
)
const DateFormat = tools.DateFormat
@ -64,9 +67,18 @@ func (e Entry) MarshalText() ([]byte, error) {
if e.Title == "" {
return []byte{}, ErrorMissingTitle
}
if e.Date == (time.Time{}) {
if e.Date.IsZero() {
return []byte{}, ErrorMissingDate
}
if _, hasId := e.Fields.Get("id"); !hasId {
host, hostErr := os.Hostname()
if hostErr != nil {
host = "localhost"
}
e.Fields = e.Fields.Set("id", fmt.Sprintf("tag:%s,%s:my-log/%s", host, time.Now().Format("2006"), uuid.NewString()))
}
ch := e.getFieldMarshalChan()
buff := &bytes.Buffer{}
buff.WriteString("@begin ")