-1

I have implemented the syslog daemon service in my golang app. I used syslog.New in main package and it works but now, I want to export it to another package.

package config

import (
    "log/syslog"
)

func LogBook() ? {
    sysLog, _ := syslog.New(syslog.LOG_LOCAL0|syslog.LOG_ERROR, "myapp") // syslog.New returns (*Writer, error)
    return ?
}

How can I implement this function? After, how can I use this variable 'sysLog' in other packages?

Thank you!

icarbajo
  • 321
  • 2
  • 5
  • 17
  • How about a *syslog.Writer ? What's the actual question because that answer is just too obvious. – Volker May 09 '17 at 09:44
  • If I do this question it's because I'm rookie in golang and the answer is not obvious for me... I don't know how I can write the function. – icarbajo May 09 '17 at 10:00
  • If new to Go the best recommendation is to take the Tour of Go twice and read through Effective Go afterwards. – Volker May 09 '17 at 10:06

1 Answers1

2

The answer is pretty simple as @Volker said,

func LogBook() *syslog.Writer {
    sysLog, _ := syslog.New(syslog.LOG_LOCAL0|syslog.LOG_ERROR, "myapp")
    return sysLog
}

Usage Example:

func main(){
    w := LogBook()
    w.Info("message")
}

Please notice:

  • This package is not implemented on Windows. As the syslog package is frozen, Windows users are encouraged to use a package outside of the standard library. For background, see https://golang.org/issue/1108.
  • This package is not implemented on Plan 9.
  • This package is not implemented on NaCl (Native Client).
Elad
  • 664
  • 1
  • 5
  • 14