mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-08-07 22:02:56 +03:00
add in-built logging support for a better dev experience
This commit is contained in:
2
vendor/github.com/aybabtme/humanlog/.gitignore
generated
vendored
Normal file
2
vendor/github.com/aybabtme/humanlog/.gitignore
generated
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
*.ignore
|
||||
dist
|
201
vendor/github.com/aybabtme/humanlog/LICENSE.md
generated
vendored
Normal file
201
vendor/github.com/aybabtme/humanlog/LICENSE.md
generated
vendored
Normal file
@@ -0,0 +1,201 @@
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "{}"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright {yyyy} {name of copyright owner}
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
75
vendor/github.com/aybabtme/humanlog/README.md
generated
vendored
Normal file
75
vendor/github.com/aybabtme/humanlog/README.md
generated
vendored
Normal file
@@ -0,0 +1,75 @@
|
||||
# humanlog
|
||||
|
||||
Read logs from `stdin` and prints them back to `stdout`, but prettier.
|
||||
|
||||
# Using it
|
||||
|
||||
[Grab a release](https://github.com/aybabtme/humanlog/releases) or :
|
||||
|
||||
## With Go installed
|
||||
```bash
|
||||
$ go get -u github.com/aybabtme/humanlog/...
|
||||
```
|
||||
|
||||
## On linux
|
||||
|
||||
```bash
|
||||
wget -qO- https://github.com/aybabtme/humanlog/releases/download/0.4.0/humanlog_Linux_x86_64.tar.gz | tar xvz
|
||||
```
|
||||
|
||||
## On OS X
|
||||
|
||||
```bash
|
||||
brew tap aybabtme/homebrew-tap
|
||||
brew install humanlog
|
||||
```
|
||||
|
||||
# Example
|
||||
|
||||
If you emit logs in JSON or in [`logfmt`](https://brandur.org/logfmt), you will enjoy pretty logs when those
|
||||
entries are encountered by `humanlog`. Unrecognized lines are left unchanged.
|
||||
|
||||
```
|
||||
$ humanlog < /var/log/logfile.log
|
||||
```
|
||||
|
||||

|
||||
|
||||
# Contributing
|
||||
|
||||
How to help:
|
||||
|
||||
* __support more log formats__: by submitting `human.Handler` implementations.
|
||||
* __live querying__: add support for filtering in log output in real time.
|
||||
* __charting__: some key-values have semantics that could be charted in real time. For
|
||||
instance, durations, frequency of numeric values, etc. See the [l2met][] project.
|
||||
|
||||
# Usage
|
||||
|
||||
```
|
||||
NAME:
|
||||
humanlog - reads structured logs from stdin, makes them pretty on stdout!
|
||||
|
||||
USAGE:
|
||||
humanlog [global options] command [command options] [arguments...]
|
||||
|
||||
VERSION:
|
||||
0.4.0
|
||||
|
||||
AUTHOR:
|
||||
Antoine Grondin - <antoine@digitalocean.com>
|
||||
|
||||
COMMANDS:
|
||||
help, h Shows a list of commands or help for one command
|
||||
|
||||
GLOBAL OPTIONS:
|
||||
--skip '--skip option --skip option' keys to skip when parsing a log entry
|
||||
--keep '--keep option --keep option' keys to keep when parsing a log entry
|
||||
--sort-longest sort by longest key after having sorted lexicographically
|
||||
--skip-unchanged skip keys that have the same value than the previous entry
|
||||
--truncate truncates values that are longer than --truncate-length
|
||||
--truncate-length '15' truncate values that are longer than this length
|
||||
--help, -h show help
|
||||
--version, -v print the version
|
||||
```
|
||||
[l2met]: https://github.com/ryandotsmith/l2met
|
29
vendor/github.com/aybabtme/humanlog/docker_compose_handler.go
generated
vendored
Normal file
29
vendor/github.com/aybabtme/humanlog/docker_compose_handler.go
generated
vendored
Normal file
@@ -0,0 +1,29 @@
|
||||
package humanlog
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
)
|
||||
|
||||
// dcLogsPrefixRe parses out a prefix like 'web_1 | ' from docker-compose
|
||||
// The regex exists of five parts:
|
||||
// 1. An optional color terminal escape sequence
|
||||
// 2. The name of the service
|
||||
// 3. Any number of spaces, and a pipe symbol
|
||||
// 4. An optional color reset escape sequence
|
||||
// 5. The rest of the line
|
||||
var dcLogsPrefixRe = regexp.MustCompile("^(?:\x1b\\[\\d+m)?(?P<service_name>[a-zA-Z0-9._-]+)\\s+\\|(?:\x1b\\[0m)? (?P<rest_of_line>.*)$")
|
||||
|
||||
type handler interface {
|
||||
TryHandle([]byte) bool
|
||||
setField(key, val []byte)
|
||||
}
|
||||
|
||||
func tryDockerComposePrefix(d []byte, nextHandler handler) bool {
|
||||
if matches := dcLogsPrefixRe.FindSubmatch(d); matches != nil {
|
||||
if nextHandler.TryHandle(matches[2]) {
|
||||
nextHandler.setField([]byte(`service`), matches[1])
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
14
vendor/github.com/aybabtme/humanlog/go.mod
generated
vendored
Normal file
14
vendor/github.com/aybabtme/humanlog/go.mod
generated
vendored
Normal file
@@ -0,0 +1,14 @@
|
||||
module github.com/aybabtme/humanlog
|
||||
|
||||
go 1.13
|
||||
|
||||
require (
|
||||
github.com/aybabtme/rgbterm v0.0.0-20170906152045-cc83f3b3ce59
|
||||
github.com/fatih/color v1.7.1-0.20180516100307-2d684516a886
|
||||
github.com/go-logfmt/logfmt v0.4.0
|
||||
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515
|
||||
github.com/mattn/go-colorable v0.1.0
|
||||
github.com/mattn/go-isatty v0.0.4 // indirect
|
||||
github.com/urfave/cli v1.20.1-0.20180226030253-8e01ec4cd3e2
|
||||
golang.org/x/sys v0.0.0-20170407050850-f3918c30c5c2 // indirect
|
||||
)
|
16
vendor/github.com/aybabtme/humanlog/go.sum
generated
vendored
Normal file
16
vendor/github.com/aybabtme/humanlog/go.sum
generated
vendored
Normal file
@@ -0,0 +1,16 @@
|
||||
github.com/aybabtme/rgbterm v0.0.0-20170906152045-cc83f3b3ce59 h1:WWB576BN5zNSZc/M9d/10pqEx5VHNhaQ/yOVAkmj5Yo=
|
||||
github.com/aybabtme/rgbterm v0.0.0-20170906152045-cc83f3b3ce59/go.mod h1:q/89r3U2H7sSsE2t6Kca0lfwTK8JdoNGS/yzM/4iH5I=
|
||||
github.com/fatih/color v1.7.1-0.20180516100307-2d684516a886 h1:NAFoy+QgUpERgK3y1xiVh5HcOvSeZHpXTTo5qnvnuK4=
|
||||
github.com/fatih/color v1.7.1-0.20180516100307-2d684516a886/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
|
||||
github.com/go-logfmt/logfmt v0.4.0 h1:MP4Eh7ZCb31lleYCFuwm0oe4/YGak+5l1vA2NOE80nA=
|
||||
github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
|
||||
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515 h1:T+h1c/A9Gawja4Y9mFVWj2vyii2bbUNDw3kt9VxK2EY=
|
||||
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
|
||||
github.com/mattn/go-colorable v0.1.0 h1:v2XXALHHh6zHfYTJ+cSkwtyffnaOyR1MXaA91mTrb8o=
|
||||
github.com/mattn/go-colorable v0.1.0/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
|
||||
github.com/mattn/go-isatty v0.0.4 h1:bnP0vzxcAdeI1zdubAl5PjU6zsERjGZb7raWodagDYs=
|
||||
github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
|
||||
github.com/urfave/cli v1.20.1-0.20180226030253-8e01ec4cd3e2 h1:xAkHCttGHKXIr10OSiFzNt0XOJyHMdng0ylSynT8sMo=
|
||||
github.com/urfave/cli v1.20.1-0.20180226030253-8e01ec4cd3e2/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
|
||||
golang.org/x/sys v0.0.0-20170407050850-f3918c30c5c2 h1:niKkabq6kYToDafvvFw9MeTkT4ifSvpOCRP6pFxOCZE=
|
||||
golang.org/x/sys v0.0.0-20170407050850-f3918c30c5c2/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
20
vendor/github.com/aybabtme/humanlog/goreleaser.yaml
generated
vendored
Normal file
20
vendor/github.com/aybabtme/humanlog/goreleaser.yaml
generated
vendored
Normal file
@@ -0,0 +1,20 @@
|
||||
build:
|
||||
main: ./cmd/humanlog/main.go
|
||||
binary: humanlog
|
||||
ldflags:
|
||||
- -s -w -X main.build={{.Version}}
|
||||
goos:
|
||||
- windows
|
||||
- darwin
|
||||
- linux
|
||||
goarch:
|
||||
- amd64
|
||||
|
||||
brews:
|
||||
- github:
|
||||
owner: aybabtme
|
||||
name: homebrew-tap
|
||||
|
||||
nfpms:
|
||||
- formats:
|
||||
- deb
|
108
vendor/github.com/aybabtme/humanlog/handler.go
generated
vendored
Normal file
108
vendor/github.com/aybabtme/humanlog/handler.go
generated
vendored
Normal file
@@ -0,0 +1,108 @@
|
||||
package humanlog
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/fatih/color"
|
||||
"github.com/kr/logfmt"
|
||||
)
|
||||
|
||||
// Handler can recognize it's log lines, parse them and prettify them.
|
||||
type Handler interface {
|
||||
CanHandle(line []byte) bool
|
||||
Prettify(skipUnchanged bool) []byte
|
||||
logfmt.Handler
|
||||
}
|
||||
|
||||
var DefaultOptions = &HandlerOptions{
|
||||
SortLongest: true,
|
||||
SkipUnchanged: true,
|
||||
Truncates: true,
|
||||
LightBg: false,
|
||||
TruncateLength: 15,
|
||||
TimeFormat: time.Stamp,
|
||||
|
||||
KeyColor: color.New(color.FgGreen),
|
||||
ValColor: color.New(color.FgHiWhite),
|
||||
TimeLightBgColor: color.New(color.FgBlack),
|
||||
TimeDarkBgColor: color.New(color.FgWhite),
|
||||
MsgLightBgColor: color.New(color.FgBlack),
|
||||
MsgAbsentLightBgColor: color.New(color.FgHiBlack),
|
||||
MsgDarkBgColor: color.New(color.FgHiWhite),
|
||||
MsgAbsentDarkBgColor: color.New(color.FgWhite),
|
||||
DebugLevelColor: color.New(color.FgMagenta),
|
||||
InfoLevelColor: color.New(color.FgCyan),
|
||||
WarnLevelColor: color.New(color.FgYellow),
|
||||
ErrorLevelColor: color.New(color.FgRed),
|
||||
PanicLevelColor: color.New(color.BgRed),
|
||||
FatalLevelColor: color.New(color.BgHiRed, color.FgHiWhite),
|
||||
UnknownLevelColor: color.New(color.FgMagenta),
|
||||
}
|
||||
|
||||
type HandlerOptions struct {
|
||||
Skip map[string]struct{}
|
||||
Keep map[string]struct{}
|
||||
SortLongest bool
|
||||
SkipUnchanged bool
|
||||
Truncates bool
|
||||
LightBg bool
|
||||
TruncateLength int
|
||||
TimeFormat string
|
||||
|
||||
KeyColor *color.Color
|
||||
ValColor *color.Color
|
||||
TimeLightBgColor *color.Color
|
||||
TimeDarkBgColor *color.Color
|
||||
MsgLightBgColor *color.Color
|
||||
MsgAbsentLightBgColor *color.Color
|
||||
MsgDarkBgColor *color.Color
|
||||
MsgAbsentDarkBgColor *color.Color
|
||||
DebugLevelColor *color.Color
|
||||
InfoLevelColor *color.Color
|
||||
WarnLevelColor *color.Color
|
||||
ErrorLevelColor *color.Color
|
||||
PanicLevelColor *color.Color
|
||||
FatalLevelColor *color.Color
|
||||
UnknownLevelColor *color.Color
|
||||
}
|
||||
|
||||
func (h *HandlerOptions) shouldShowKey(key string) bool {
|
||||
if len(h.Keep) != 0 {
|
||||
if _, keep := h.Keep[key]; keep {
|
||||
return true
|
||||
}
|
||||
}
|
||||
if len(h.Skip) != 0 {
|
||||
if _, skip := h.Skip[key]; skip {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (h *HandlerOptions) shouldShowUnchanged(key string) bool {
|
||||
if len(h.Keep) != 0 {
|
||||
if _, keep := h.Keep[key]; keep {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (h *HandlerOptions) SetSkip(skip []string) {
|
||||
if h.Skip == nil {
|
||||
h.Skip = make(map[string]struct{})
|
||||
}
|
||||
for _, key := range skip {
|
||||
h.Skip[key] = struct{}{}
|
||||
}
|
||||
}
|
||||
|
||||
func (h *HandlerOptions) SetKeep(keep []string) {
|
||||
if h.Keep == nil {
|
||||
h.Keep = make(map[string]struct{})
|
||||
}
|
||||
for _, key := range keep {
|
||||
h.Keep[key] = struct{}{}
|
||||
}
|
||||
}
|
261
vendor/github.com/aybabtme/humanlog/json_handler.go
generated
vendored
Normal file
261
vendor/github.com/aybabtme/humanlog/json_handler.go
generated
vendored
Normal file
@@ -0,0 +1,261 @@
|
||||
package humanlog
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"math"
|
||||
"sort"
|
||||
"strings"
|
||||
"text/tabwriter"
|
||||
"time"
|
||||
|
||||
"github.com/fatih/color"
|
||||
)
|
||||
|
||||
// JSONHandler can handle logs emmited by logrus.TextFormatter loggers.
|
||||
type JSONHandler struct {
|
||||
buf *bytes.Buffer
|
||||
out *tabwriter.Writer
|
||||
truncKV int
|
||||
|
||||
Opts *HandlerOptions
|
||||
|
||||
Level string
|
||||
Time time.Time
|
||||
Message string
|
||||
Fields map[string]string
|
||||
|
||||
last map[string]string
|
||||
}
|
||||
|
||||
func checkEachUntilFound(fieldList []string, found func(string) bool) bool {
|
||||
for _, field := range fieldList {
|
||||
if found(field) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// supportedTimeFields enumerates supported timestamp field names
|
||||
var supportedTimeFields = []string{"time", "ts", "@timestamp", "timestamp"}
|
||||
|
||||
// supportedMessageFields enumarates supported Message field names
|
||||
var supportedMessageFields = []string{"message", "msg"}
|
||||
|
||||
// supportedLevelFields enumarates supported level field names
|
||||
var supportedLevelFields = []string{"level", "lvl", "loglevel"}
|
||||
|
||||
func (h *JSONHandler) clear() {
|
||||
h.Level = ""
|
||||
h.Time = time.Time{}
|
||||
h.Message = ""
|
||||
h.last = h.Fields
|
||||
h.Fields = make(map[string]string)
|
||||
if h.buf != nil {
|
||||
h.buf.Reset()
|
||||
}
|
||||
}
|
||||
|
||||
// TryHandle tells if this line was handled by this handler.
|
||||
func (h *JSONHandler) TryHandle(d []byte) bool {
|
||||
if !h.UnmarshalJSON(d) {
|
||||
h.clear()
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// UnmarshalJSON sets the fields of the handler.
|
||||
func (h *JSONHandler) UnmarshalJSON(data []byte) bool {
|
||||
raw := make(map[string]interface{})
|
||||
err := json.Unmarshal(data, &raw)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
checkEachUntilFound(supportedTimeFields, func(field string) bool {
|
||||
time, ok := tryParseTime(raw[field])
|
||||
if ok {
|
||||
h.Time = time
|
||||
delete(raw, field)
|
||||
}
|
||||
return ok
|
||||
})
|
||||
|
||||
checkEachUntilFound(supportedMessageFields, func(field string) bool {
|
||||
msg, ok := raw[field].(string)
|
||||
if ok {
|
||||
h.Message = msg
|
||||
delete(raw, field)
|
||||
}
|
||||
return ok
|
||||
})
|
||||
|
||||
checkEachUntilFound(supportedLevelFields, func(field string) bool {
|
||||
lvl, ok := raw[field]
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
if strLvl, ok := lvl.(string); ok {
|
||||
h.Level = strLvl
|
||||
} else if flLvl, ok := lvl.(float64); ok {
|
||||
h.Level = convertBunyanLogLevel(flLvl)
|
||||
} else {
|
||||
h.Level = "???"
|
||||
}
|
||||
delete(raw, field)
|
||||
return true
|
||||
})
|
||||
|
||||
if h.Fields == nil {
|
||||
h.Fields = make(map[string]string)
|
||||
}
|
||||
|
||||
for key, val := range raw {
|
||||
switch v := val.(type) {
|
||||
case float64:
|
||||
if v-math.Floor(v) < 0.000001 && v < 1e9 {
|
||||
// looks like an integer that's not too large
|
||||
h.Fields[key] = fmt.Sprintf("%d", int(v))
|
||||
} else {
|
||||
h.Fields[key] = fmt.Sprintf("%g", v)
|
||||
}
|
||||
case string:
|
||||
h.Fields[key] = fmt.Sprintf("%q", v)
|
||||
default:
|
||||
h.Fields[key] = fmt.Sprintf("%v", v)
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
func (h *JSONHandler) setField(key, val []byte) {
|
||||
if h.Fields == nil {
|
||||
h.Fields = make(map[string]string)
|
||||
}
|
||||
h.Fields[string(key)] = string(val)
|
||||
}
|
||||
|
||||
// Prettify the output in a logrus like fashion.
|
||||
func (h *JSONHandler) Prettify(skipUnchanged bool) []byte {
|
||||
defer h.clear()
|
||||
if h.out == nil {
|
||||
if h.Opts == nil {
|
||||
h.Opts = DefaultOptions
|
||||
}
|
||||
h.buf = bytes.NewBuffer(nil)
|
||||
h.out = tabwriter.NewWriter(h.buf, 0, 1, 0, '\t', 0)
|
||||
}
|
||||
|
||||
var (
|
||||
msgColor *color.Color
|
||||
msgAbsentColor *color.Color
|
||||
)
|
||||
if h.Opts.LightBg {
|
||||
msgColor = h.Opts.MsgLightBgColor
|
||||
msgAbsentColor = h.Opts.MsgAbsentLightBgColor
|
||||
} else {
|
||||
msgColor = h.Opts.MsgDarkBgColor
|
||||
msgAbsentColor = h.Opts.MsgAbsentDarkBgColor
|
||||
}
|
||||
msgColor = color.New(color.FgHiWhite)
|
||||
msgAbsentColor = color.New(color.FgHiWhite)
|
||||
|
||||
var msg string
|
||||
if h.Message == "" {
|
||||
msg = msgAbsentColor.Sprint("<no msg>")
|
||||
} else {
|
||||
msg = msgColor.Sprint(h.Message)
|
||||
}
|
||||
|
||||
lvl := strings.ToUpper(h.Level)[:imin(4, len(h.Level))]
|
||||
var level string
|
||||
switch h.Level {
|
||||
case "debug":
|
||||
level = h.Opts.DebugLevelColor.Sprint(lvl)
|
||||
case "info":
|
||||
level = h.Opts.InfoLevelColor.Sprint(lvl)
|
||||
case "warn", "warning":
|
||||
level = h.Opts.WarnLevelColor.Sprint(lvl)
|
||||
case "error":
|
||||
level = h.Opts.ErrorLevelColor.Sprint(lvl)
|
||||
case "fatal", "panic":
|
||||
level = h.Opts.FatalLevelColor.Sprint(lvl)
|
||||
default:
|
||||
level = h.Opts.UnknownLevelColor.Sprint(lvl)
|
||||
}
|
||||
|
||||
var timeColor *color.Color
|
||||
if h.Opts.LightBg {
|
||||
timeColor = h.Opts.TimeLightBgColor
|
||||
} else {
|
||||
timeColor = h.Opts.TimeDarkBgColor
|
||||
}
|
||||
_, _ = fmt.Fprintf(h.out, "%s |%s| %s\t %s",
|
||||
timeColor.Sprint(h.Time.Format(h.Opts.TimeFormat)),
|
||||
level,
|
||||
msg,
|
||||
strings.Join(h.joinKVs(skipUnchanged, "="), "\t "),
|
||||
)
|
||||
|
||||
_ = h.out.Flush()
|
||||
|
||||
return h.buf.Bytes()
|
||||
}
|
||||
|
||||
func (h *JSONHandler) joinKVs(skipUnchanged bool, sep string) []string {
|
||||
|
||||
kv := make([]string, 0, len(h.Fields))
|
||||
for k, v := range h.Fields {
|
||||
if !h.Opts.shouldShowKey(k) {
|
||||
continue
|
||||
}
|
||||
|
||||
if skipUnchanged {
|
||||
if lastV, ok := h.last[k]; ok && lastV == v && !h.Opts.shouldShowUnchanged(k) {
|
||||
continue
|
||||
}
|
||||
}
|
||||
kstr := h.Opts.KeyColor.Sprint(k)
|
||||
|
||||
var vstr string
|
||||
if h.Opts.Truncates && len(v) > h.Opts.TruncateLength {
|
||||
vstr = v[:h.Opts.TruncateLength] + "..."
|
||||
} else {
|
||||
vstr = v
|
||||
}
|
||||
vstr = h.Opts.ValColor.Sprint(vstr)
|
||||
kv = append(kv, kstr+sep+vstr)
|
||||
}
|
||||
|
||||
sort.Strings(kv)
|
||||
|
||||
if h.Opts.SortLongest {
|
||||
sort.Stable(byLongest(kv))
|
||||
}
|
||||
|
||||
return kv
|
||||
}
|
||||
|
||||
// convertBunyanLogLevel returns a human readable log level given a numerical bunyan level
|
||||
// https://github.com/trentm/node-bunyan#levels
|
||||
func convertBunyanLogLevel(level float64) string {
|
||||
switch level {
|
||||
case 10:
|
||||
return "trace"
|
||||
case 20:
|
||||
return "debug"
|
||||
case 30:
|
||||
return "info"
|
||||
case 40:
|
||||
return "warn"
|
||||
case 50:
|
||||
return "error"
|
||||
case 60:
|
||||
return "fatal"
|
||||
default:
|
||||
return "???"
|
||||
}
|
||||
}
|
239
vendor/github.com/aybabtme/humanlog/logfmt_handler.go
generated
vendored
Normal file
239
vendor/github.com/aybabtme/humanlog/logfmt_handler.go
generated
vendored
Normal file
@@ -0,0 +1,239 @@
|
||||
package humanlog
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"text/tabwriter"
|
||||
"time"
|
||||
|
||||
"github.com/fatih/color"
|
||||
"github.com/go-logfmt/logfmt"
|
||||
)
|
||||
|
||||
// LogfmtHandler can handle logs emmited by logrus.TextFormatter loggers.
|
||||
type LogfmtHandler struct {
|
||||
buf *bytes.Buffer
|
||||
out *tabwriter.Writer
|
||||
truncKV int
|
||||
|
||||
Opts *HandlerOptions
|
||||
|
||||
Level string
|
||||
Time time.Time
|
||||
Message string
|
||||
Fields map[string]string
|
||||
|
||||
last map[string]string
|
||||
}
|
||||
|
||||
func (h *LogfmtHandler) clear() {
|
||||
h.Level = ""
|
||||
h.Time = time.Time{}
|
||||
h.Message = ""
|
||||
h.last = h.Fields
|
||||
h.Fields = make(map[string]string)
|
||||
if h.buf != nil {
|
||||
h.buf.Reset()
|
||||
}
|
||||
}
|
||||
|
||||
// CanHandle tells if this line can be handled by this handler.
|
||||
func (h *LogfmtHandler) TryHandle(d []byte) bool {
|
||||
if !bytes.ContainsRune(d, '=') {
|
||||
return false
|
||||
}
|
||||
|
||||
if !h.UnmarshalLogfmt(d) {
|
||||
h.clear()
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// HandleLogfmt sets the fields of the handler.
|
||||
func (h *LogfmtHandler) UnmarshalLogfmt(data []byte) bool {
|
||||
dec := logfmt.NewDecoder(bytes.NewReader(data))
|
||||
for dec.ScanRecord() {
|
||||
next_kv:
|
||||
for dec.ScanKeyval() {
|
||||
key := dec.Key()
|
||||
val := dec.Value()
|
||||
if h.Time.IsZero() {
|
||||
foundTime := checkEachUntilFound(supportedTimeFields, func(field string) bool {
|
||||
time, ok := tryParseTime(string(val))
|
||||
if ok {
|
||||
h.Time = time
|
||||
}
|
||||
return ok
|
||||
})
|
||||
if foundTime {
|
||||
continue next_kv
|
||||
}
|
||||
}
|
||||
|
||||
if len(h.Message) == 0 {
|
||||
foundMessage := checkEachUntilFound(supportedMessageFields, func(field string) bool {
|
||||
if !bytes.Equal(key, []byte(field)) {
|
||||
return false
|
||||
}
|
||||
h.Message = string(val)
|
||||
return true
|
||||
})
|
||||
if foundMessage {
|
||||
continue next_kv
|
||||
}
|
||||
}
|
||||
|
||||
if len(h.Level) == 0 {
|
||||
foundLevel := checkEachUntilFound(supportedLevelFields, func(field string) bool {
|
||||
if !bytes.Equal(key, []byte(field)) {
|
||||
return false
|
||||
}
|
||||
h.Level = string(val)
|
||||
return true
|
||||
})
|
||||
if foundLevel {
|
||||
continue next_kv
|
||||
}
|
||||
}
|
||||
|
||||
h.setField(key, val)
|
||||
}
|
||||
}
|
||||
return dec.Err() == nil
|
||||
}
|
||||
|
||||
// Prettify the output in a logrus like fashion.
|
||||
func (h *LogfmtHandler) Prettify(skipUnchanged bool) []byte {
|
||||
defer h.clear()
|
||||
if h.out == nil {
|
||||
if h.Opts == nil {
|
||||
h.Opts = DefaultOptions
|
||||
}
|
||||
h.buf = bytes.NewBuffer(nil)
|
||||
h.out = tabwriter.NewWriter(h.buf, 0, 1, 0, '\t', 0)
|
||||
}
|
||||
|
||||
var (
|
||||
msgColor *color.Color
|
||||
msgAbsentColor *color.Color
|
||||
)
|
||||
if h.Opts.LightBg {
|
||||
msgColor = h.Opts.MsgLightBgColor
|
||||
msgAbsentColor = h.Opts.MsgAbsentLightBgColor
|
||||
} else {
|
||||
msgColor = h.Opts.MsgDarkBgColor
|
||||
msgAbsentColor = h.Opts.MsgAbsentDarkBgColor
|
||||
}
|
||||
|
||||
var msg string
|
||||
if h.Message == "" {
|
||||
msg = msgAbsentColor.Sprint("<no msg>")
|
||||
} else {
|
||||
msg = msgColor.Sprint(h.Message)
|
||||
}
|
||||
|
||||
lvl := strings.ToUpper(h.Level)[:imin(4, len(h.Level))]
|
||||
var level string
|
||||
switch h.Level {
|
||||
case "debug":
|
||||
level = h.Opts.DebugLevelColor.Sprint(lvl)
|
||||
case "info":
|
||||
level = h.Opts.InfoLevelColor.Sprint(lvl)
|
||||
case "warn", "warning":
|
||||
level = h.Opts.WarnLevelColor.Sprint(lvl)
|
||||
case "error":
|
||||
level = h.Opts.ErrorLevelColor.Sprint(lvl)
|
||||
case "fatal", "panic":
|
||||
level = h.Opts.FatalLevelColor.Sprint(lvl)
|
||||
default:
|
||||
level = h.Opts.UnknownLevelColor.Sprint(lvl)
|
||||
}
|
||||
|
||||
var timeColor *color.Color
|
||||
if h.Opts.LightBg {
|
||||
timeColor = h.Opts.TimeLightBgColor
|
||||
} else {
|
||||
timeColor = h.Opts.TimeDarkBgColor
|
||||
}
|
||||
_, _ = fmt.Fprintf(h.out, "%s |%s| %s\t %s",
|
||||
timeColor.Sprint(h.Time.Format(h.Opts.TimeFormat)),
|
||||
level,
|
||||
msg,
|
||||
strings.Join(h.joinKVs(skipUnchanged, "="), "\t "),
|
||||
)
|
||||
|
||||
_ = h.out.Flush()
|
||||
|
||||
return h.buf.Bytes()
|
||||
}
|
||||
|
||||
func (h *LogfmtHandler) setLevel(val []byte) { h.Level = string(val) }
|
||||
func (h *LogfmtHandler) setMessage(val []byte) { h.Message = string(val) }
|
||||
func (h *LogfmtHandler) setTime(val []byte) (parsed bool) {
|
||||
valStr := string(val)
|
||||
if valFloat, err := strconv.ParseFloat(valStr, 64); err == nil {
|
||||
h.Time, parsed = tryParseTime(valFloat)
|
||||
} else {
|
||||
h.Time, parsed = tryParseTime(string(val))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (h *LogfmtHandler) setField(key, val []byte) {
|
||||
if h.Fields == nil {
|
||||
h.Fields = make(map[string]string)
|
||||
}
|
||||
h.Fields[string(key)] = string(val)
|
||||
}
|
||||
|
||||
func (h *LogfmtHandler) joinKVs(skipUnchanged bool, sep string) []string {
|
||||
|
||||
kv := make([]string, 0, len(h.Fields))
|
||||
for k, v := range h.Fields {
|
||||
if !h.Opts.shouldShowKey(k) {
|
||||
continue
|
||||
}
|
||||
|
||||
if skipUnchanged {
|
||||
if lastV, ok := h.last[k]; ok && lastV == v && !h.Opts.shouldShowUnchanged(k) {
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
kstr := h.Opts.KeyColor.Sprint(k)
|
||||
|
||||
var vstr string
|
||||
if h.Opts.Truncates && len(v) > h.Opts.TruncateLength {
|
||||
vstr = v[:h.Opts.TruncateLength] + "..."
|
||||
} else {
|
||||
vstr = v
|
||||
}
|
||||
vstr = h.Opts.ValColor.Sprint(vstr)
|
||||
kv = append(kv, kstr+sep+vstr)
|
||||
}
|
||||
|
||||
sort.Strings(kv)
|
||||
|
||||
if h.Opts.SortLongest {
|
||||
sort.Stable(byLongest(kv))
|
||||
}
|
||||
|
||||
return kv
|
||||
}
|
||||
|
||||
type byLongest []string
|
||||
|
||||
func (s byLongest) Len() int { return len(s) }
|
||||
func (s byLongest) Less(i, j int) bool { return len(s[i]) < len(s[j]) }
|
||||
func (s byLongest) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
|
||||
|
||||
func imin(a, b int) int {
|
||||
if a < b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
68
vendor/github.com/aybabtme/humanlog/scanner.go
generated
vendored
Normal file
68
vendor/github.com/aybabtme/humanlog/scanner.go
generated
vendored
Normal file
@@ -0,0 +1,68 @@
|
||||
package humanlog
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"io"
|
||||
)
|
||||
|
||||
var (
|
||||
eol = [...]byte{'\n'}
|
||||
)
|
||||
|
||||
// Scanner reads logfmt'd lines from src and prettify them onto dst.
|
||||
// If the lines aren't logfmt, it will simply write them out with no
|
||||
// prettification.
|
||||
func Scanner(src io.Reader, dst io.Writer, opts *HandlerOptions) error {
|
||||
in := bufio.NewScanner(src)
|
||||
in.Split(bufio.ScanLines)
|
||||
|
||||
var line uint64
|
||||
|
||||
var lastLogfmt bool
|
||||
var lastJSON bool
|
||||
|
||||
logfmtEntry := LogfmtHandler{Opts: opts}
|
||||
jsonEntry := JSONHandler{Opts: opts}
|
||||
|
||||
for in.Scan() {
|
||||
line++
|
||||
lineData := in.Bytes()
|
||||
|
||||
// remove that pesky syslog crap
|
||||
lineData = bytes.TrimPrefix(lineData, []byte("@cee: "))
|
||||
|
||||
switch {
|
||||
|
||||
case jsonEntry.TryHandle(lineData):
|
||||
dst.Write(jsonEntry.Prettify(opts.SkipUnchanged && lastJSON))
|
||||
lastJSON = true
|
||||
|
||||
case logfmtEntry.TryHandle(lineData):
|
||||
dst.Write(logfmtEntry.Prettify(opts.SkipUnchanged && lastLogfmt))
|
||||
lastLogfmt = true
|
||||
|
||||
case tryDockerComposePrefix(lineData, &jsonEntry):
|
||||
dst.Write(jsonEntry.Prettify(opts.SkipUnchanged && lastJSON))
|
||||
lastJSON = true
|
||||
|
||||
case tryDockerComposePrefix(lineData, &logfmtEntry):
|
||||
dst.Write(logfmtEntry.Prettify(opts.SkipUnchanged && lastLogfmt))
|
||||
lastLogfmt = true
|
||||
|
||||
default:
|
||||
lastLogfmt = false
|
||||
lastJSON = false
|
||||
dst.Write(lineData)
|
||||
}
|
||||
dst.Write(eol[:])
|
||||
|
||||
}
|
||||
|
||||
switch err := in.Err(); err {
|
||||
case nil, io.EOF:
|
||||
return nil
|
||||
default:
|
||||
return err
|
||||
}
|
||||
}
|
66
vendor/github.com/aybabtme/humanlog/time_parse.go
generated
vendored
Normal file
66
vendor/github.com/aybabtme/humanlog/time_parse.go
generated
vendored
Normal file
@@ -0,0 +1,66 @@
|
||||
package humanlog
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
var formats = []string{
|
||||
"2006-01-02 15:04:05.999999999 -0700 MST",
|
||||
"2006-01-02 15:04:05",
|
||||
time.RFC3339,
|
||||
time.RFC3339Nano,
|
||||
time.RFC822,
|
||||
time.RFC822Z,
|
||||
time.RFC850,
|
||||
time.RFC1123,
|
||||
time.RFC1123Z,
|
||||
time.UnixDate,
|
||||
time.RubyDate,
|
||||
time.ANSIC,
|
||||
time.Kitchen,
|
||||
time.Stamp,
|
||||
time.StampMilli,
|
||||
time.StampMicro,
|
||||
time.StampNano,
|
||||
}
|
||||
|
||||
func parseTimeFloat64(value float64) time.Time {
|
||||
v := int64(value)
|
||||
switch {
|
||||
case v > 1e18:
|
||||
case v > 1e15:
|
||||
v *= 1e3
|
||||
case v > 1e12:
|
||||
v *= 1e6
|
||||
default:
|
||||
return time.Unix(v, 0)
|
||||
}
|
||||
|
||||
return time.Unix(v/1e9, v%1e9)
|
||||
}
|
||||
|
||||
// tries to parse time using a couple of formats before giving up
|
||||
func tryParseTime(value interface{}) (time.Time, bool) {
|
||||
var t time.Time
|
||||
var err error
|
||||
switch value.(type) {
|
||||
case string:
|
||||
for _, layout := range formats {
|
||||
t, err = time.Parse(layout, value.(string))
|
||||
if err == nil {
|
||||
return t, true
|
||||
}
|
||||
}
|
||||
case float32:
|
||||
return parseTimeFloat64(float64(value.(float32))), true
|
||||
case float64:
|
||||
return parseTimeFloat64(value.(float64)), true
|
||||
case int:
|
||||
return parseTimeFloat64(float64(value.(int))), true
|
||||
case int32:
|
||||
return parseTimeFloat64(float64(value.(int32))), true
|
||||
case int64:
|
||||
return parseTimeFloat64(float64(value.(int64))), true
|
||||
}
|
||||
return t, false
|
||||
}
|
Reference in New Issue
Block a user