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

1
go.mod
View file

@ -5,6 +5,7 @@ go 1.21.5
require (
github.com/BurntSushi/toml v1.3.2
github.com/caarlos0/env/v10 v10.0.0
github.com/google/uuid v1.6.0
github.com/markusmobius/go-dateparser v1.2.3
github.com/mitchellh/mapstructure v1.5.0
github.com/spf13/cobra v1.8.0

2
go.sum
View file

@ -8,6 +8,8 @@ github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/elliotchance/pie/v2 v2.7.0 h1:FqoIKg4uj0G/CrLGuMS9ejnFKa92lxE1dEgBD3pShXg=
github.com/elliotchance/pie/v2 v2.7.0/go.mod h1:18t0dgGFH006g4eVdDtWfgFZPQEgl10IoEO8YWEq3Og=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/hablullah/go-hijri v1.0.2 h1:drT/MZpSZJQXo7jftf5fthArShcaMtsal0Zf/dnmp6k=
github.com/hablullah/go-hijri v1.0.2/go.mod h1:OS5qyYLDjORXzK4O1adFw9Q5WfhOcMdAKglDkcTxgWQ=
github.com/hablullah/go-juliandays v1.0.0 h1:A8YM7wIj16SzlKT0SRJc9CD29iiaUzpBLzh5hr0/5p0=

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 ")