Skip to content

Instantly share code, notes, and snippets.

@tj
Last active September 23, 2017 15:02
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save tj/a96740cd614179fdd3d8ae7dc17610cc to your computer and use it in GitHub Desktop.
package main
func main() {
log := NewLogger()
log.Info("Foo")
log.Info("Bar")
log.Error("Boom")
}
func NewLogger() Logger {
return Logger{
prefix: "==> ",
}
}
type Logger struct {
prefix string
}
// Info level log message.
func (l *Logger) Info(msg string) {
l.log("INFO", msg)
}
// Error level log message.
func (l *Logger) Error(msg string) {
l.log("ERROR", msg)
}
// log helper.
func (l *Logger) log(level, msg string) {
println(l.prefix + level + " " + msg)
}
// Package main.
const main = (function(){
const print = console.log
const println = console.log
function main() {
let log = NewLogger()
log.Info("Foo")
log.Info("Bar")
log.Error("Boom")
}
function NewLogger() {
return new Logger({
prefix: "==> "
})
}
function Logger({ prefix }) {
this.prefix = prefix
}
Logger.prototype.Info = function(msg) {
const l = this
l.log("INFO", msg)
}
Logger.prototype.Error = function(msg) {
const l = this
l.log("ERROR", msg)
}
Logger.prototype.log = function(level, msg) {
const l = this
println(l.prefix + level + " " + msg)
}
return {
main,
NewLogger
}
})();
main.main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment