Git Product home page Git Product logo

golok's Introduction

Golok

Golok is a logger for golang, for easy logging in your programs. Golok here to help you learn more about what's happening within your application, Golok provides robust logging services that allow you to log messages to files, the system error log, and sentry

Golok inspired by laravel logger facades.

Install

$go get -u -v github.com/nurcahyo/golok

Use go get -u to update the package.

Configuration

To config you just need a map. You can use json file and unmarshall it to map[string]interface{} or just use map[string]interface{} on your code. Example config:

var config = map[string]interface{}{
	"default": "stack",
	"channels": map[string]interface{}{
		"stack": map[string]interface{}{
			"channels": []string{"system", "file"},
			"driver":   "stack",
		},
		"system": map[string]interface{}{
			"level":  "debug",
			"driver": "syslog",
		},
		"file": map[string]interface{}{
			"level":    "debug",
			"daily":    false,
			"filename": "log",
			"path":     "./",
			"driver":   "file",
		},
		"sentry": map[string]interface{}{
			"level":       "error",
			"driver":      "sentry",
			"environment": "golok-test",
			"wait":        true,
			"dsn":         "https://772b29d6912f4efba93f82716ecfbb6f:[email protected]/1407424",
		},
	},
}

Available Channel Drivers

Name Description
stack A wrapper to facilitate creating "multi-channel" channels
syslog A handler to print error log to stderr or to your console output
file A handler to print error log to file
sentry A handler to print error log to sentry

Configuring stack channel

As previously mentioned, the stack driver allows you to combine multiple channels into a single log channel. To illustrate how to use log stacks, let's take a look at an example configuration that you might see in a production application:

On the example config above you can see this code part.

...
"stack": map[string]interface{}{
  "channels": []string{"system", "file"},
  "driver":   "stack",
}
...

Let's dissect this configuration. First, notice our stack channel aggregates two other channels via its channels option: system and file. So, when logging messages, both of these channels will have the opportunity to log the message.

Writing Log Message

You may write information to the logs using golok package. The logger provides the eight logging levels defined:

  • Debug
  • Info
  • Warning
  • Error
  • Critical

To write the log message you need to import package first

import github.com/nurcahyo/golok

After import write the config for example use the example config on Configuring Slack channel sections above. And then initialize the config

golok.Initialize(config)

After that you can call log level function directly from any source that import the golok package.

golok.Debug("test log")
golok.Error(errors.New("Iam test log"))
golok.Stack([]string{"sentry"}).Debug("Test stack with sentry log below config level")
golok.Stack([]string{"sentry"}).Error(errors.New("Test stack with sentry log pushed"))

For full example see test file golok_test

Sentry

As you see in configuration and available driver, we have sentry driver. Here is example output of sentry driver in sentry dashboard

Future Development Plan

  • Add comment block to every public methods
  • Improve code test file
  • Decorate syslog output
  • Add ability to write log to Specific Channels for example: golok.Only("file").Debug("Write to file")
  • Add New Relic Driver
  • Add Slack Notification Driver
  • Add contextual information for example: golok.Info("Create post failed", map[string]interface{"user_id": 1}))

Contribute

  • Fork this repo and create a Pull Request
  • Dont forget to write a test file

golok's People

Contributors

nurcahyo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.