sirupsen / logrus

Structured, pluggable logging for Go.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Collect logs from os pipe

sfl0r3nz05 opened this issue · comments

Hello logrus experts,

I need to collect each of the logs issued by logrus and then apply a sha256 and a base64 as part of the code that generates the log itself, this (base64(sha256(log))) associated with the unique identifier (uuid) of each log, as a key-value, will allow to ensure the integrity of the log when I send it to an ELK infrastructure.

image

  • Does the library include a way to do this, i.e., to collect the log that has been emitted within a string?

  • If not, Could it be done via os.pipe?

Thanks in advance,

Santiago.

You could do that with a hook.
It can iterate an all fields and the message and other field you want to check to generate such a hash.
AFAIK, nobody has already implemented such a hook though.

Hi @dgsb, in order to solve the issue I have package the logrus log into a captureOutput function, as you can see below:
image

However, now when the sha256 and base64 are calculated for each of the logs, the result is always the same, as you will see in the figure below.
image

I am adding the generated logs, highlighting the logs with the same uuid as the previous image:
image

I presume that the problem is in the function to capture the logs (captureOutput):

`package main

import (
"io"
"os"
"log"
"sync"
"bytes"
)

func captureOutput(f func()) string {
reader, writer, err := os.Pipe()
if err != nil {
panic(err)
}
stdout := os.Stdout
stderr := os.Stderr
defer func() {
os.Stdout = stdout
os.Stderr = stderr
log.SetOutput(os.Stderr)
}()
os.Stdout = writer
os.Stderr = writer
log.SetOutput(writer)
out := make(chan string)
wg := new(sync.WaitGroup)
wg.Add(1)
go func() {
var buf bytes.Buffer
wg.Done()
io.Copy(&buf, reader)
out <- buf.String()
}()
wg.Wait()
f()
writer.Close()
return <-out
}`

I will appreciate any suggestions,

Santiago

I have an update @dgsb. I have tried to reproduce the problem here and when I use log.Println instead of logrus, the base64 is generated properly:

image
image

However, when I set logrus the problem appears again:

image
image

So, my point is, why am I not able to collect logs using logrus?

Note: In the examples I am using other log structure. #

I don't understand what your problem is but:

  • it is probably unrelated to logrus
  • the uuid isn't changing because you're capturing its value in the closure
  • if you eventually find a logrus bug, we'll be happy to help, but you need to provide at least a minimal code sample which exhibits the problem

This issue is stale because it has been open for 30 days with no activity.

This issue was closed because it has been inactive for 14 days since being marked as stale.