0

I run a server which redirects the log content to a file, say "current.log" for debugging purpose which I cannot show to the user. But I got a scenario where I need to show a specific set of logs to the user so that they can follow up the process going on in the back end. So I tried writing another set of logs(in a custom file) for each user(one log file per user) which i need to show to the user which does not contain any secure data.

2 Answers 2

2

One way to solve this is to use logger mechanism where you can create an object for your logger(with specific file location) and then write logs using that object. So that, the logs written using the specific object will be redirected to that particular file.

The logger package that I used is "github.com/sadlil/gologger"

Sample code:

package main
import (
     "github.com/sadlil/gologger"
)

func main() {
    logger := gologger.GetLogger(gologger.FILE, "/home/user/path/user.log")
    logger.Log("Test file log")
}

Note: file will be automatically created at the time of object creation

so you can create a logger object for each user dynamically(each user gets a different log file) and logs wil be redirected to that file. The logging format will be

[LOG] [2016-04-07 11:31:28] [main::test.go::main] [8] Test file log

Sign up to request clarification or add additional context in comments.

Comments

2

You can use lumberjack.v2, with it you can define a custom log file for each user.

In the following snippet I use a simple boolean to determine if the log content should be added to the log file or not for each user.

package main

import (
    "gopkg.in/natefinch/lumberjack.v2"
    "io/ioutil"
    "log"
    "strconv"
)

type user struct {
    id     int
    logger *log.Logger
}

func createUser(id int, logWanted bool) user {
    var l *log.Logger

    if logWanted {
        // Here the log content will be added in the user log file
        userFIle := &lumberjack.Logger{
            Filename:   "user_log_" + strconv.Itoa(id) + ".log",
            MaxSize:    250, // mb
            MaxBackups: 5,
            MaxAge:     10, // in days
        }
        l = log.New(userFIle, "User: ", log.Ldate|log.Ltime|log.Lshortfile)
    } else {
        // Here the log content will go nowhere
        l = log.New(ioutil.Discard, "User: ", log.Ldate|log.Ltime|log.Lshortfile)
    }
    return user{id, l}
}

func doSomething(u user) {
    u.logger.Printf("Log content: user id %v \n", u.id)
}

func main() {
    user1 := createUser(1, true)
    user2 := createUser(2, false)
    user3 := createUser(3, true)
    doSomething(user1)
    doSomething(user2)
    doSomething(user3)
}

This will create a new set of rolling log files for each user with the log turned on.

  • "user_log_1.log" for the user1
  • "user_log_3.log" for the user3
  • and no log file for the user2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.