• Stars
    star
    261
  • Rank 156,602 (Top 4 %)
  • Language
    Go
  • License
    MIT License
  • Created over 10 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

So you always leave a note

jWalterWeatherman

Seamless printing to the terminal (stdout) and logging to a io.Writer (file) that’s as easy to use as fmt.Println.

and_that__s_why_you_always_leave_a_note_by_jonnyetc-d57q7um Graphic by JonnyEtc

JWW is primarily a wrapper around the excellent standard log library. It provides a few advantages over using the standard log library alone.

  1. Ready to go out of the box.
  2. One library for both printing to the terminal and logging (to files).
  3. Really easy to log to either a temp file or a file you specify.

I really wanted a very straightforward library that could seamlessly do the following things.

  1. Replace all the println, printf, etc statements thoughout my code with something more useful
  2. Allow the user to easily control what levels are printed to stdout
  3. Allow the user to easily control what levels are logged
  4. Provide an easy mechanism (like fmt.Println) to print info to the user which can be easily logged as well
  5. Due to 2 & 3 provide easy verbose mode for output and logs
  6. Not have any unnecessary initialization cruft. Just use it.

Usage

Step 1. Use it

Put calls throughout your source based on type of feedback. No initialization or setup needs to happen. Just start calling things.

Available Loggers are:

  • TRACE
  • DEBUG
  • INFO
  • WARN
  • ERROR
  • CRITICAL
  • FATAL

These each are loggers based on the log standard library and follow the standard usage. Eg.

    import (
        jww "github.com/spf13/jwalterweatherman"
    )

    ...

    if err != nil {

        // This is a pretty serious error and the user should know about
        // it. It will be printed to the terminal as well as logged under the
        // default thresholds.

        jww.ERROR.Println(err)
    }

    if err2 != nil {
        // This error isn’t going to materially change the behavior of the
        // application, but it’s something that may not be what the user
        // expects. Under the default thresholds, Warn will be logged, but
        // not printed to the terminal. 

        jww.WARN.Println(err2)
    }

    // Information that’s relevant to what’s happening, but not very
    // important for the user. Under the default thresholds this will be
    // discarded.

    jww.INFO.Printf("information %q", response)

NOTE: You can also use the library in a non-global setting by creating an instance of a Notebook:

notepad = jww.NewNotepad(jww.LevelInfo, jww.LevelTrace, os.Stdout, ioutil.Discard, "", log.Ldate|log.Ltime)
notepad.WARN.Println("Some warning"")

Why 7 levels?

Maybe you think that 7 levels are too much for any application... and you are probably correct. Just because there are seven levels doesn’t mean that you should be using all 7 levels. Pick the right set for your needs. Remember they only have to mean something to your project.

Step 2. Optionally configure JWW

Under the default thresholds :

  • Debug, Trace & Info goto /dev/null
  • Warn and above is logged (when a log file/io.Writer is provided)
  • Error and above is printed to the terminal (stdout)

Changing the thresholds

The threshold can be changed at any time, but will only affect calls that execute after the change was made.

This is very useful if your application has a verbose mode. Of course you can decide what verbose means to you or even have multiple levels of verbosity.

    import (
        jww "github.com/spf13/jwalterweatherman"
    )

    if Verbose {
        jww.SetLogThreshold(jww.LevelTrace)
        jww.SetStdoutThreshold(jww.LevelInfo)
    }

Note that JWW's own internal output uses log levels as well, so set the log level before making any other calls if you want to see what it's up to.

Setting a log file

JWW can log to any io.Writer:

    jww.SetLogOutput(customWriter) 

More information

This is an early release. I’ve been using it for a while and this is the third interface I’ve tried. I like this one pretty well, but no guarantees that it won’t change a bit.

I wrote this for use in hugo. If you are looking for a static website engine that’s super fast please checkout Hugo.

More Repositories

1

cobra

A Commander for modern Go CLI interactions
Go
37,720
star
2

viper

Go configuration with fangs
Go
27,043
star
3

spf13-vim

The ultimate vim distribution
Vim Script
15,558
star
4

afero

A FileSystem Abstraction System for Go
Go
5,913
star
5

cast

safe and easy casting from one type to another in Go
Go
3,073
star
6

cobra-cli

Cobra CLI tool to generate applications and commands
Go
695
star
7

hyde

Port of Mdo's excellent theme to Hugo
CSS
553
star
8

nitro

Quick and easy performance analyzer library for golang
Go
300
star
9

PIV

PHP Integration environment for Vim
Vim Script
251
star
10

spf13.com

Hugo Based website of Steve Francia
CSS
143
star
11

dagobah

dagobah is an awesome RSS feed aggregator & reader written in Go inspired by planet
Go
136
star
12

kaiju

Open source discussion server
JavaScript
113
star
13

herring-cove

Port of arnp's excellent theme to Hugo
CSS
43
star
14

vim-colors

Collection of color schemes for VIM
Vim Script
40
star
15

mongoProducts

Sample data for Mongo Product Presentation
37
star
16

tri

Todo CLI app
Go
32
star
17

dotfiles

spf13's dotfiles
Shell
27
star
18

firstGoApp-Planet

Built for OSCON2014 .. Each step is in it's own branch
Go
20
star
19

Vogon

A MongoDB PHP based blog
JavaScript
16
star
20

Milieu

Location based check in application powered by MongoDB written in Ruby
Ruby
13
star
21

cobra.dev

Cobra website
HTML
8
star
22

relativeTime

Golang relative date formatting (1 day ago, in 2 weeks), etc.
7
star
23

stevefrancia.com

Homepage Website for SteveFrancia.com
HTML
6
star
24

wp_GitHub_Code_Viewer

Plugin for Wordpress to insert file from github into a blog post
PHP
5
star
25

spf13

5
star
26

spf13.github.com

Git Hub Page for spf13
4
star
27

vim-gocode

Vim Plugin of nsf/gocode for use with Vundle/Pathogen
Vim Script
3
star
28

vimbrant

Harnessing the power of vim to turn source into highlighted html
3
star
29

One-More-For-The-Road

A Skeleton App for Sinatra using MongoDB
JavaScript
3
star
30

instant

Instant theme for Hugo (derived from Black Tie Instant)
HTML
2
star
31

Autostamp

Auto update the time and version stamp in the header for VIM
Vim Script
1
star
32

YourFirstRubyMongoDBApp

Skeleton for your first ruby MongoDB app to go along with my workshop
Ruby
1
star