Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

slog: follow ups #2

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: CI

on:
push:
branches:
- "master"
pull_request:
branches:
- "*"

defaults:
run:
shell: bash

env:
# go needs absolute directories, using the $HOME variable doesn't work here.
GOCACHE: /home/runner/work/go/pkg/build
GOPATH: /home/runner/work/go
GO_VERSION: 1.22.3

jobs:
########################
# run unit tests
########################
unit-test:
name: run unit tests
runs-on: ubuntu-latest
steps:
- name: git checkout
uses: actions/checkout@v3

- name: go cache
uses: actions/cache@v3
with:
path: /home/runner/work/go
key: btclog-${{ runner.os }}-go-${{ env.GO_VERSION }}-${{ github.job }}-${{ hashFiles('**/go.sum') }}
restore-keys: |
btclog-${{ runner.os }}-go-${{ env.GO_VERSION }}-${{ github.job }}-${{ hashFiles('**/go.sum') }}
btclog-${{ runner.os }}-go-${{ env.GO_VERSION }}-${{ github.job }}-
btclog-${{ runner.os }}-go-${{ env.GO_VERSION }}-
btclog-${{ runner.os }}-go-

- name: setup go ${{ env.GO_VERSION }}
uses: actions/setup-go@v3
with:
go-version: '~${{ env.GO_VERSION }}'

- name: run unit tests
run: make unit
11 changes: 11 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
PKG := github.com/lightninglabs/btclog

GOLIST := go list -deps $(PKG)/... | grep '$(PKG)'| grep -v '/vendor/'
GOTEST := go test -v

XARGS := xargs -L 1
UNIT := $(GOLIST) | $(XARGS) env $(GOTEST) $(TEST_FLAGS)

unit:
@$(call print, "Running unit tests.")
$(UNIT)
79 changes: 60 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,38 +1,79 @@
btclog
======

[![Build Status](http://img.shields.io/travis/btcsuite/btclog.svg)](https://travis-ci.org/btcsuite/btclog)
[![ISC License](http://img.shields.io/badge/license-ISC-blue.svg)](http://copyfree.org)
[![GoDoc](https://img.shields.io/badge/godoc-reference-blue.svg)](http://godoc.org/github.com/btcsuite/btclog)
Forked and adapted from https://github.com/btcsuite/btclog.

Package btclog defines a logger interface and provides a default implementation
of a subsystem-aware leveled logger implementing the same interface.

## Installation

```bash
$ go get github.com/btcsuite/btclog
$ go get github.com/lightninglabs/btclog
```

## GPG Verification Key
## Usage

All official release tags are signed by Conformal so users can ensure the code
has not been tampered with and is coming from the btcsuite developers. To
verify the signature perform the following:
`btclog.NewSLogger` can be used to construct a new `btclog.Logger` type which
can then be used for logging calls. The `NewSLogger` function expects to be
initialised with a type that implements the `btclog.Handler` interface which is
responsible for writing logging records to a backend writer. Callers may provide
their own `Handler` implementations (for example, the standard library `slog`
package provides some handler implementations such as a JSON Handler and a Text
Handler) or else they may use the default one provided with this package:
`DefaultHandler`.

- Download the public key from the Conformal website at
https://opensource.conformal.com/GIT-GPG-KEY-conformal.txt
Example Usage:

- Import the public key into your GPG keyring:
```bash
gpg --import GIT-GPG-KEY-conformal.txt
```
```
// Create a new DefaultHandler that writes to stdout and set the
// logging level to Trace.
handler := btclog.NewDefaultHandler(os.Stdout)
handler.SetLevel(btclog.LevelTrace)

// Use the above handler to construct a new logger.
log := btclog.NewSLogger(handler)

/*
2024-09-18 11:53:03.287 [INF]: An info level log
*/
log.Info("An info level log")

// Create a subsystem logger with no timestamps.
handler = btclog.NewDefaultHandler(os.Stdout, btclog.WithNoTimestamp())
log = btclog.NewSLogger(handler.SubSystem("SUBS"))

/*
[INF] SUBS: An info level log
*/
log.Info("An info level log")

- Verify the release tag with the following command where `TAG_NAME` is a
placeholder for the specific tag:
```bash
git tag -v TAG_NAME
```
// Include log source.
handler = btclog.NewDefaultHandler(
os.Stdout,
btclog.WithCallerFlags(btclog.Lshortfile),
btclog.WithNoTimestamp(),
)
log = btclog.NewSLogger(handler.SubSystem("SUBS"))

/*
[INF] SUBS main.go:36: An info level log
*/
log.Info("An info level log")

// Attach attributes to a context. This will result in log lines
// including these attributes if the context is passed to them. Also
// pass in an attribute at log time.
log = btclog.NewSLogger(btclog.NewDefaultHandler(
os.Stdout, btclog.WithNoTimestamp(),
).SubSystem("SUBS"))
ctx := btclog.WithCtx(context.Background(), "request_id", 5)

/*
[INF] SUBS: A log line with context values request_id=5 another_key=another_value
*/
log.InfoS(ctx, "A log line with context values", "another_key", "another_value")
```

## License

Expand Down
27 changes: 0 additions & 27 deletions doc.go

This file was deleted.

138 changes: 0 additions & 138 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (
"sync"
"sync/atomic"
"time"
"unicode"
"unicode/utf8"
)

const (
Expand Down Expand Up @@ -336,139 +334,3 @@ func appendTextValue(buf *buffer, v slog.Value) {
appendString(buf, fmt.Sprintf("%s", v))
}
}

// Copied from log/slog/text_handler.go.
//
// needsQuoting returns true if the given strings should be wrapped in quotes.
func needsQuoting(s string) bool {
if len(s) == 0 {
return true
}
for i := 0; i < len(s); {
b := s[i]
if b < utf8.RuneSelf {
// Quote anything except a backslash that would need
// quoting in a JSON string, as well as space and '='.
if b != '\\' && (b == ' ' || b == '=' || !safeSet[b]) {
return true
}
i++
continue
}
r, size := utf8.DecodeRuneInString(s[i:])
if r == utf8.RuneError || unicode.IsSpace(r) ||
!unicode.IsPrint(r) {

return true
}
i += size
}
return false
}

// Copied from encoding/json/tables.go.
//
// safeSet holds the value true if the ASCII character with the given array
// position can be represented inside a JSON string without any further
// escaping.
//
// All values are true except for the ASCII control characters (0-31), the
// double quote ("), and the backslash character ("\").
var safeSet = [utf8.RuneSelf]bool{
' ': true,
'!': true,
'"': false,
'#': true,
'$': true,
'%': true,
'&': true,
'\'': true,
'(': true,
')': true,
'*': true,
'+': true,
',': true,
'-': true,
'.': true,
'/': true,
'0': true,
'1': true,
'2': true,
'3': true,
'4': true,
'5': true,
'6': true,
'7': true,
'8': true,
'9': true,
':': true,
';': true,
'<': true,
'=': true,
'>': true,
'?': true,
'@': true,
'A': true,
'B': true,
'C': true,
'D': true,
'E': true,
'F': true,
'G': true,
'H': true,
'I': true,
'J': true,
'K': true,
'L': true,
'M': true,
'N': true,
'O': true,
'P': true,
'Q': true,
'R': true,
'S': true,
'T': true,
'U': true,
'V': true,
'W': true,
'X': true,
'Y': true,
'Z': true,
'[': true,
'\\': false,
']': true,
'^': true,
'_': true,
'`': true,
'a': true,
'b': true,
'c': true,
'd': true,
'e': true,
'f': true,
'g': true,
'h': true,
'i': true,
'j': true,
'k': true,
'l': true,
'm': true,
'n': true,
'o': true,
'p': true,
'q': true,
'r': true,
's': true,
't': true,
'u': true,
'v': true,
'w': true,
'x': true,
'y': true,
'z': true,
'{': true,
'|': true,
'}': true,
'~': true,
'\u007f': true,
}
Loading
Loading