~samwhited/testlog

e7c6a33df36ad4af27fa38b09628bad853b85165 — Sam Whited 1 year, 5 months ago 81aa893 v0.0.1
Initial public release
6 files changed, 126 insertions(+), 0 deletions(-)

A CONTRIBUTING.md
A LICENSE
A README.md
A go.mod
A testlog.go
A testlog_test.go
A CONTRIBUTING.md => CONTRIBUTING.md +31 -0
@@ 0,0 1,31 @@
## Submitting Patches

To submit a patch, first learn to use `git send-email` by reading
[git-send-email.io], then read the SourceHut [mailing list etiquette] guide.
You can send patches to my general purpose patches [mailing list].

Please prefix the subject with `[PATCH testlog]`.
To configure your checkout of this repo to always use the correct prefix and
send to the correct list cd into the repo and run:

    git config sendemail.to ~samwhited/patches@lists.sr.ht
    git config format.subjectPrefix 'PATCH testlog'

[git-send-email.io]: https://git-send-email.io/
[mailing list etiquette]: https://man.sr.ht/lists.sr.ht/etiquette.md
[mailing list]: https://lists.sr.ht/~samwhited/patches


## License

Licensed under the BSD 2 Clause License ([LICENSE] or
https://opensource.org/licenses/BSD-2-Clause)

[LICENSE]: https://git.sr.ht/~samwhited/testlog/tree/master/LICENSE.md


### Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted
for inclusion in the work by you shall be licensed as above, without any
additional terms or conditions.

A LICENSE => LICENSE +23 -0
@@ 0,0 1,23 @@
Copyright © 2014 The Soquee Contributors.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

A README.md => README.md +27 -0
@@ 0,0 1,27 @@
# testlog

The **testlog** package provides a convenient way to log to the test output when
testing projects that log to a logger (such as the standard libraries
[`"net/http".Server`]).
This ensures that log output doesn't pollute test output and is shown under the
correct test only if the test that generated the log output fails.

```go
import (
	"code.soquee.net/testlog"
)
```

[`"net/http".Server`]: https://godoc.org/net/http#Server.ErrorLog


## License

The package may be used under the terms of the BSD 2-Clause License a copy of
which may be found in the [`LICENSE`] file.

Unless you explicitly state otherwise, any contribution submitted for inclusion
in the work by you shall be licensed as above, without any additional terms or
conditions.

[`LICENSE`]: ./LICENSE

A go.mod => go.mod +3 -0
@@ 0,0 1,3 @@
module code.soquee.net/testlog

go 1.11

A testlog.go => testlog.go +28 -0
@@ 0,0 1,28 @@
// Package testlog is a log.Logger that proxies to the Log function on a
// testing.T.
//
// It is used to group log messages under the tests that generated them in test
// output and to only show those messages if the test that generated them
// failed.
package testlog // import "code.soquee.net/testlog"

import (
	"log"
	"testing"
)

// New returns a new logger that logs to the provided testing.T.
func New(t testing.TB) *log.Logger {
	t.Helper()
	return log.New(testWriter{TB: t}, t.Name()+" ", log.LstdFlags|log.Lshortfile|log.LUTC)
}

type testWriter struct {
	testing.TB
}

func (tw testWriter) Write(p []byte) (int, error) {
	tw.Helper()
	tw.Logf("%s", p)
	return len(p), nil
}

A testlog_test.go => testlog_test.go +14 -0
@@ 0,0 1,14 @@
package testlog_test

import (
	"code.soquee.net/testlog"
)

import (
	"testing"
)

func TestLog(t *testing.T) {
	logger := testlog.New(t)
	logger.Println("Logging should not cause a test failure")
}