<commit_before>package realtime

import (
	"time"

	"github.com/cozy/cozy-stack/pkg/prefixer"
	"github.com/sirupsen/logrus"
)

type log struct {
	Time    time.Time              `json:"time"`
	Message string                 `json:"message"`
	Level   string                 `json:"level"`
	Data    map[string]interface{} `json:"data"`

	id      string
	docType string
}

func (l *log) DocType() string   { return l.docType }
func (l *log) ID() string        { return l.id }
func (l *log) Rev() string       { return "" }
func (l *log) SetID(id string)   {}
func (l *log) SetRev(rev string) {}

type logHook struct {
	Hub
	db      prefixer.Prefixer
	docType string
	docID   string
}

// LogHook creates a hook that transmits logs through redis pubsub
// messaging.
func LogHook(db prefixer.Prefixer, hub Hub, parentDocType, documentID string) logrus.Hook {
	return &logHook{
		Hub:     hub,
		db:      db,
		docType: parentDocType + ".logs",
		docID:   documentID,
	}
}

func (r *logHook) Levels() []logrus.Level {
	return []logrus.Level{
		logrus.DebugLevel,
		logrus.InfoLevel,
		logrus.WarnLevel,
		logrus.ErrorLevel,
		logrus.FatalLevel,
		logrus.PanicLevel,
	}
}

func (r *logHook) Fire(entry *logrus.Entry) error {
	doc := &log{
		Time:    entry.Time,
		Message: entry.Message,
		Level:   entry.Level.String(),
		Data:    entry.Data,
		docType: r.docType,
		id:      r.docID,
	}
	r.Hub.Publish(r.db, EventCreate, doc, nil)
	return nil
}<commit_msg>Fix realtime events by settings log id on an _id field<commit_after>package realtime

import (
	"time"

	"github.com/cozy/cozy-stack/pkg/prefixer"
	"github.com/sirupsen/logrus"
)

type log struct {
	LogID   string                 `json:"_id"`
	Time    time.Time              `json:"time"`
	Message string                 `json:"message"`
	Level   string                 `json:"level"`
	Data    map[string]interface{} `json:"data"`

	docType string
}

func (l *log) DocType() string   { return l.docType }
func (l *log) ID() string        { return l.LogID }
func (l *log) Rev() string       { return "" }
func (l *log) SetID(id string)   { l.LogID = id }
func (l *log) SetRev(rev string) {}

type logHook struct {
	Hub
	db      prefixer.Prefixer
	docType string
	docID   string
}

// LogHook creates a hook that transmits logs through redis pubsub
// messaging.
func LogHook(db prefixer.Prefixer, hub Hub, parentDocType, documentID string) logrus.Hook {
	return &logHook{
		Hub:     hub,
		db:      db,
		docType: parentDocType + ".logs",
		docID:   documentID,
	}
}

func (r *logHook) Levels() []logrus.Level {
	return []logrus.Level{
		logrus.DebugLevel,
		logrus.InfoLevel,
		logrus.WarnLevel,
		logrus.ErrorLevel,
		logrus.FatalLevel,
		logrus.PanicLevel,
	}
}

func (r *logHook) Fire(entry *logrus.Entry) error {
	doc := &log{
		LogID:   r.docID,
		Time:    entry.Time,
		Message: entry.Message,
		Level:   entry.Level.String(),
		Data:    entry.Data,
		docType: r.docType,
	}
	r.Hub.Publish(r.db, EventCreate, doc, nil)
	return nil
}
{'fraction_non_alphanumeric': 0.12813650050184008, 'fraction_numerical': 0.0, 'mean_word_length': 2.8983050847457625, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1648078', 'n_tokens_mistral': 1124, 'n_tokens_neox': 1080, 'n_words': 283}