├── .gitignore ├── .travis.yml ├── LICENSE ├── Makefile ├── README.md ├── config.go ├── filter.go ├── filter_test.go ├── fluent.go ├── fluent_test.go ├── reflect.go ├── reflect_test.go └── wercker.yml /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files, Static and Dynamic libs (Shared Objects) 2 | *.o 3 | *.a 4 | *.so 5 | 6 | # Folders 7 | _obj 8 | _test 9 | 10 | # Architecture specific extensions/prefixes 11 | *.[568vq] 12 | [568vq].out 13 | 14 | *.cgo1.go 15 | *.cgo2.c 16 | _cgo_defun.c 17 | _cgo_gotypes.go 18 | _cgo_export.* 19 | 20 | _testmain.go 21 | 22 | *.exe 23 | *.test 24 | *.prof 25 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: go 3 | go: 4 | - 1.11 5 | - "1.12.x" 6 | - tip 7 | matrix: 8 | allow_failures: 9 | - go: tip 10 | before_install: 11 | - go get github.com/axw/gocov/gocov 12 | - go get github.com/mattn/goveralls 13 | - go get golang.org/x/tools/cmd/cover 14 | - go get -t -v ./... 15 | - make lint 16 | script: 17 | - make test 18 | after_success: 19 | - | 20 | if [[ $TRAVIS_GO_VERSION = 1.12.x ]] 21 | then 22 | make coverage 23 | fi 24 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | .PHONY: lint test 2 | 3 | lint: 4 | @type golangci-lint > /dev/null || go get -u github.com/golangci/golangci-lint/cmd/golangci-lint 5 | golangci-lint -E gofmt run ./... 6 | 7 | test: 8 | go test ./... 9 | 10 | coverage: 11 | go test -covermode=count -coverprofile=coverage.txt ./... 12 | @type goveralls > /dev/null || go get -u github.com/mattn/goveralls 13 | goveralls -coverprofile=coverage.txt -service=travis-ci 14 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Fluentd Hook for Logrus :walrus: 2 | ---- 3 | 4 | [![GoDoc][1]][2] [![License: Apache 2.0][3]][4] [![Release][5]][6] [![Travis Status][7]][8] [![wercker Status][19]][20] [![Coveralls Coverage][9]][10] [![Go Report Card][13]][14] [![Downloads][15]][16] [![Code Climate][21]][22] [![BCH compliance][23]][24] 5 | 6 | [1]: https://godoc.org/github.com/evalphobia/logrus_fluent?status.svg 7 | [2]: https://godoc.org/github.com/evalphobia/logrus_fluent 8 | [3]: https://img.shields.io/badge/License-Apache%202.0-blue.svg 9 | [4]: LICENSE.md 10 | [5]: https://img.shields.io/github/release/evalphobia/logrus_fluent.svg 11 | [6]: https://github.com/evalphobia/logrus_fluent/releases/latest 12 | [7]: https://travis-ci.org/evalphobia/logrus_fluent.svg?branch=master 13 | [8]: https://travis-ci.org/evalphobia/logrus_fluent 14 | [9]: https://coveralls.io/repos/evalphobia/logrus_fluent/badge.svg?branch=master&service=github 15 | [10]: https://coveralls.io/github/evalphobia/logrus_fluent?branch=master 16 | [11]: https://codecov.io/github/evalphobia/logrus_fluent/coverage.svg?branch=master 17 | [12]: https://codecov.io/github/evalphobia/logrus_fluent?branch=master 18 | [13]: https://goreportcard.com/badge/github.com/evalphobia/logrus_fluent 19 | [14]: https://goreportcard.com/report/github.com/evalphobia/logrus_fluent 20 | [15]: https://img.shields.io/github/downloads/evalphobia/logrus_fluent/total.svg?maxAge=1800 21 | [16]: https://github.com/evalphobia/logrus_fluent/releases 22 | [17]: https://img.shields.io/github/stars/evalphobia/logrus_fluent.svg 23 | [18]: https://github.com/evalphobia/logrus_fluent/stargazers 24 | [19]: https://app.wercker.com/status/04fb4bde79d8c54bb681af664394d2e4/s/master 25 | [20]: https://app.wercker.com/project/byKey/04fb4bde79d8c54bb681af664394d2e4 26 | [21]: https://codeclimate.com/github/evalphobia/logrus_fluent/badges/gpa.svg 27 | [22]: https://codeclimate.com/github/evalphobia/logrus_fluent 28 | [23]: https://bettercodehub.com/edge/badge/evalphobia/logrus_fluent?branch=master 29 | [24]: https://bettercodehub.com/ 30 | 31 | 32 | 33 | ## Usage 34 | 35 | ```go 36 | import ( 37 | "github.com/sirupsen/logrus" 38 | "github.com/evalphobia/logrus_fluent" 39 | ) 40 | 41 | func main() { 42 | hook, err := logrus_fluent.NewWithConfig(logrus_fluent.Config{ 43 | Host: "localhost", 44 | Port: 24224, 45 | }) 46 | if err != nil { 47 | panic(err) 48 | } 49 | 50 | // set custom fire level 51 | hook.SetLevels([]logrus.Level{ 52 | logrus.PanicLevel, 53 | logrus.ErrorLevel, 54 | }) 55 | 56 | // set static tag 57 | hook.SetTag("original.tag") 58 | 59 | // ignore field 60 | hook.AddIgnore("context") 61 | 62 | // filter func 63 | hook.AddFilter("error", logrus_fluent.FilterError) 64 | 65 | logrus.AddHook(hook) 66 | } 67 | 68 | func logging(ctx context.Context) { 69 | logrus.WithFields(logrus.Fields{ 70 | "value": "some content...", 71 | "error": errors.New("unknown error"), // this field will be applied filter function in the hook. 72 | "context": ctx, // this field will be ignored in the hook. 73 | }).Error("error message") 74 | } 75 | ``` 76 | 77 | 78 | ## Special fields 79 | 80 | Some logrus fields have a special meaning in this hook. 81 | 82 | - `tag` is used as a fluentd tag. (if `tag` is omitted, Entry.Message is used as a fluentd tag, unless a static tag is set for the hook with `hook.SetTag`) 83 | -------------------------------------------------------------------------------- /config.go: -------------------------------------------------------------------------------- 1 | package logrus_fluent 2 | 3 | import ( 4 | "time" 5 | 6 | "github.com/fluent/fluent-logger-golang/fluent" 7 | "github.com/sirupsen/logrus" 8 | ) 9 | 10 | // Config is settings for FluentHook. 11 | type Config struct { 12 | Port int 13 | Host string 14 | LogLevels []logrus.Level 15 | DisableConnectionPool bool // Fluent client will be created every logging if true. 16 | DefaultTag string 17 | DefaultMessageField string 18 | DefaultIgnoreFields map[string]struct{} 19 | DefaultFilters map[string]func(interface{}) interface{} 20 | 21 | // from fluent.Config 22 | // see https://github.com/fluent/fluent-logger-golang/blob/master/fluent/fluent.go 23 | FluentNetwork string 24 | FluentSocketPath string 25 | Timeout time.Duration 26 | WriteTimeout time.Duration 27 | BufferLimit int 28 | RetryWait int 29 | MaxRetry int 30 | TagPrefix string 31 | AsyncConnect bool 32 | MarshalAsJSON bool 33 | SubSecondPrecision bool 34 | RequestAck bool 35 | } 36 | 37 | // FluentConfig converts data to fluent.Config. 38 | func (c Config) FluentConfig() fluent.Config { 39 | return fluent.Config{ 40 | FluentPort: c.Port, 41 | FluentHost: c.Host, 42 | FluentNetwork: c.FluentNetwork, 43 | FluentSocketPath: c.FluentSocketPath, 44 | Timeout: c.Timeout, 45 | WriteTimeout: c.WriteTimeout, 46 | BufferLimit: c.BufferLimit, 47 | RetryWait: c.RetryWait, 48 | MaxRetry: c.MaxRetry, 49 | TagPrefix: c.TagPrefix, 50 | Async: c.AsyncConnect, 51 | MarshalAsJSON: c.MarshalAsJSON, 52 | SubSecondPrecision: c.SubSecondPrecision, 53 | RequestAck: c.RequestAck, 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /filter.go: -------------------------------------------------------------------------------- 1 | package logrus_fluent 2 | 3 | // FilterError is a filter function to convert error type to string type. 4 | func FilterError(v interface{}) interface{} { 5 | if err, ok := v.(error); ok { 6 | return err.Error() 7 | } 8 | return v 9 | } 10 | -------------------------------------------------------------------------------- /filter_test.go: -------------------------------------------------------------------------------- 1 | package logrus_fluent 2 | 3 | import ( 4 | "errors" 5 | "fmt" 6 | "testing" 7 | ) 8 | 9 | func TestFilterError(t *testing.T) { 10 | 11 | tests := []struct { 12 | data interface{} 13 | isError bool 14 | }{ 15 | {errors.New("error message"), true}, 16 | {fmt.Errorf("error message"), true}, 17 | {&myError{}, true}, 18 | {1, false}, 19 | {1.0, false}, 20 | {"string value", false}, 21 | } 22 | 23 | for _, tt := range tests { 24 | target := fmt.Sprintf("%#v", tt) 25 | result := FilterError(tt.data) 26 | switch { 27 | case tt.isError: 28 | err := tt.data.(error) 29 | if result != err.Error() { 30 | t.Errorf("result should be error message: %s", target) 31 | } 32 | default: 33 | if result != tt.data { 34 | t.Errorf("result should be same as the original data: %s", target) 35 | } 36 | } 37 | } 38 | } 39 | 40 | type myError struct{} 41 | 42 | func (myError) Error() string { return "myError's Error" } 43 | -------------------------------------------------------------------------------- /fluent.go: -------------------------------------------------------------------------------- 1 | package logrus_fluent 2 | 3 | import ( 4 | "github.com/fluent/fluent-logger-golang/fluent" 5 | "github.com/sirupsen/logrus" 6 | ) 7 | 8 | const ( 9 | // TagName is struct field tag name. 10 | // Some basic option is allowed in the field tag, 11 | // 12 | // type myStruct { 13 | // Value1: `fluent:"value_1"` // change field name. 14 | // Value2: `fluent:"-"` // always omit this field. 15 | // Value3: `fluent:",omitempty"` // omit this field when zero-value. 16 | // } 17 | TagName = "fluent" 18 | // TagField is logrus field name used as fluentd tag 19 | TagField = "tag" 20 | // MessageField is logrus field name used as message. 21 | // If missing in the log fields, entry.Message is set to this field. 22 | MessageField = "message" 23 | ) 24 | 25 | var defaultLevels = []logrus.Level{ 26 | logrus.PanicLevel, 27 | logrus.FatalLevel, 28 | logrus.ErrorLevel, 29 | logrus.WarnLevel, 30 | logrus.InfoLevel, 31 | } 32 | 33 | // FluentHook is logrus hook for fluentd. 34 | type FluentHook struct { 35 | // Fluent is actual fluentd logger. 36 | // If set, this logger is used for logging. 37 | // otherwise new logger is created every time. 38 | Fluent *fluent.Fluent 39 | conf Config 40 | 41 | levels []logrus.Level 42 | tag *string 43 | 44 | messageField string 45 | ignoreFields map[string]struct{} 46 | filters map[string]func(interface{}) interface{} 47 | customizers []func(entry *logrus.Entry, data logrus.Fields) 48 | } 49 | 50 | // New returns initialized logrus hook for fluentd with persistent fluentd logger. 51 | func New(host string, port int) (*FluentHook, error) { 52 | return NewWithConfig(Config{ 53 | Host: host, 54 | Port: port, 55 | DefaultMessageField: MessageField, 56 | }) 57 | } 58 | 59 | // NewWithConfig returns initialized logrus hook by config setting. 60 | func NewWithConfig(conf Config) (*FluentHook, error) { 61 | var fd *fluent.Fluent 62 | if !conf.DisableConnectionPool { 63 | var err error 64 | fd, err = fluent.New(conf.FluentConfig()) 65 | if err != nil { 66 | return nil, err 67 | } 68 | } 69 | 70 | hook := &FluentHook{ 71 | Fluent: fd, 72 | conf: conf, 73 | levels: conf.LogLevels, 74 | ignoreFields: make(map[string]struct{}), 75 | filters: make(map[string]func(interface{}) interface{}), 76 | } 77 | // set default values 78 | if len(hook.levels) == 0 { 79 | hook.levels = defaultLevels 80 | } 81 | if conf.DefaultTag != "" { 82 | tag := conf.DefaultTag 83 | hook.tag = &tag 84 | } 85 | if conf.DefaultMessageField != "" { 86 | hook.messageField = conf.DefaultMessageField 87 | } 88 | for k, v := range conf.DefaultIgnoreFields { 89 | hook.ignoreFields[k] = v 90 | } 91 | for k, v := range conf.DefaultFilters { 92 | hook.filters[k] = v 93 | } 94 | 95 | return hook, nil 96 | } 97 | 98 | // NewHook returns initialized logrus hook for fluentd. 99 | // (** deperecated: use New() or NewWithConfig() **) 100 | func NewHook(host string, port int) *FluentHook { 101 | hook, _ := NewWithConfig(Config{ 102 | Host: host, 103 | Port: port, 104 | DefaultMessageField: MessageField, 105 | DisableConnectionPool: true, 106 | }) 107 | return hook 108 | } 109 | 110 | // Levels returns logging level to fire this hook. 111 | func (hook *FluentHook) Levels() []logrus.Level { 112 | return hook.levels 113 | } 114 | 115 | // SetLevels sets logging level to fire this hook. 116 | func (hook *FluentHook) SetLevels(levels []logrus.Level) { 117 | hook.levels = levels 118 | } 119 | 120 | // Tag returns custom static tag. 121 | func (hook *FluentHook) Tag() string { 122 | if hook.tag == nil { 123 | return "" 124 | } 125 | 126 | return *hook.tag 127 | } 128 | 129 | // SetTag sets custom static tag to override tag in the message fields. 130 | func (hook *FluentHook) SetTag(tag string) { 131 | hook.tag = &tag 132 | } 133 | 134 | // SetMessageField sets custom message field. 135 | func (hook *FluentHook) SetMessageField(messageField string) { 136 | hook.messageField = messageField 137 | } 138 | 139 | // AddIgnore adds field name to ignore. 140 | func (hook *FluentHook) AddIgnore(name string) { 141 | hook.ignoreFields[name] = struct{}{} 142 | } 143 | 144 | // AddFilter adds a custom filter function. 145 | func (hook *FluentHook) AddFilter(name string, fn func(interface{}) interface{}) { 146 | hook.filters[name] = fn 147 | } 148 | 149 | // AddCustomizer adds a custom function to modify data. 150 | func (hook *FluentHook) AddCustomizer(fn func(entry *logrus.Entry, data logrus.Fields)) { 151 | hook.customizers = append(hook.customizers, fn) 152 | } 153 | 154 | // Fire is invoked by logrus and sends log to fluentd logger. 155 | func (hook *FluentHook) Fire(entry *logrus.Entry) error { 156 | var logger *fluent.Fluent 157 | var err error 158 | 159 | switch { 160 | case hook.Fluent != nil: 161 | logger = hook.Fluent 162 | default: 163 | logger, err = fluent.New(hook.conf.FluentConfig()) 164 | if err != nil { 165 | return err 166 | } 167 | defer logger.Close() 168 | } 169 | 170 | // Create a map for passing to FluentD 171 | data := make(logrus.Fields) 172 | for k, v := range entry.Data { 173 | if _, ok := hook.ignoreFields[k]; ok { 174 | continue 175 | } 176 | if fn, ok := hook.filters[k]; ok { 177 | v = fn(v) 178 | } 179 | data[k] = v 180 | } 181 | 182 | setLevelString(entry, data) 183 | tag := hook.getTagAndDel(entry, data) 184 | if tag != entry.Message { 185 | hook.setMessage(entry, data) 186 | } 187 | 188 | // modify data to your own needs. 189 | for _, fn := range hook.customizers { 190 | fn(entry, data) 191 | } 192 | 193 | fluentData := ConvertToValue(data, TagName) 194 | err = logger.PostWithTime(tag, entry.Time, fluentData) 195 | return err 196 | } 197 | 198 | // getTagAndDel extracts tag data from log entry and custom log fields. 199 | // 1. if tag is set in the hook, use it. 200 | // 2. if tag is set in custom fields, use it. 201 | // 3. if cannot find tag data, use entry.Message as tag. 202 | func (hook *FluentHook) getTagAndDel(entry *logrus.Entry, data logrus.Fields) string { 203 | // use static tag from 204 | if hook.tag != nil { 205 | return *hook.tag 206 | } 207 | 208 | tagField, ok := data[TagField] 209 | if !ok { 210 | return entry.Message 211 | } 212 | 213 | tag, ok := tagField.(string) 214 | if !ok { 215 | return entry.Message 216 | } 217 | 218 | // remove tag from data fields 219 | delete(data, TagField) 220 | return tag 221 | } 222 | 223 | func (hook *FluentHook) setMessage(entry *logrus.Entry, data logrus.Fields) { 224 | if _, ok := data[hook.messageField]; ok { 225 | return 226 | } 227 | 228 | var v interface{} = entry.Message 229 | if fn, ok := hook.filters[hook.messageField]; ok { 230 | v = fn(v) 231 | } 232 | data[hook.messageField] = v 233 | } 234 | 235 | func setLevelString(entry *logrus.Entry, data logrus.Fields) { 236 | data["level"] = entry.Level.String() 237 | } 238 | -------------------------------------------------------------------------------- /fluent_test.go: -------------------------------------------------------------------------------- 1 | package logrus_fluent 2 | 3 | import ( 4 | "bufio" 5 | "fmt" 6 | "io" 7 | "net" 8 | "strings" 9 | "testing" 10 | 11 | "github.com/sirupsen/logrus" 12 | "github.com/stretchr/testify/assert" 13 | ) 14 | 15 | var ( 16 | // used for persistent mock server 17 | data = make(chan string) 18 | mockPort int 19 | ) 20 | 21 | const ( 22 | defaultLoopCount = 10 // assertion count 23 | testHOST = "localhost" 24 | ) 25 | 26 | // test data and assertion 27 | const ( 28 | fieldValue = "data" 29 | assertFieldValue = "value\xa4data" 30 | 31 | fieldTag = "debug.test" 32 | assertFieldTag = "\xa3tag\xaadebug.test" 33 | assertFieldTagAsFluentTag = "\x94\xaadebug.test\xd2" 34 | 35 | fieldMessage = "FieldMessage" 36 | assertFieldMessage = "\xa7message\xacFieldMessage" 37 | 38 | entryMessage = "MyEntryMessage" 39 | assertEntryMessage = "\xa7message\xaeMyEntryMessage" 40 | assertEntryMessageAsFluentTag = "\x94\xaeMyEntryMessage\xd2" 41 | 42 | staticTag = "STATIC_TAG" 43 | assertStaticTagAsFluentTag = "\x94\xaaSTATIC_TAG\xd2" 44 | ) 45 | 46 | func TestNew(t *testing.T) { 47 | _, port := newMockServer(t, nil) 48 | hook, err := New(testHOST, port) 49 | switch { 50 | case err != nil: 51 | t.Errorf("error on New: %s", err.Error()) 52 | case hook == nil: 53 | t.Errorf("hook should not be nil") 54 | case len(hook.levels) != len(defaultLevels): 55 | t.Errorf("hook.levels should be defaultLevels") 56 | case hook.Fluent == nil: 57 | t.Errorf("hook.Fluent should not be nil") 58 | case hook.messageField != MessageField: 59 | t.Errorf("hook.messageField should be %s", MessageField) 60 | } 61 | } 62 | 63 | func TestNewWithConfig(t *testing.T) { 64 | _, port := newMockServer(t, nil) 65 | conf := Config{ 66 | Host: testHOST, 67 | Port: port, 68 | DefaultMessageField: "DefaultMessageField", 69 | DefaultIgnoreFields: map[string]struct{}{"ignored": {}}, 70 | DefaultFilters: map[string]func(interface{}) interface{}{ 71 | "filtered": func(x interface{}) interface{} { 72 | return x 73 | }, 74 | }, 75 | } 76 | hook, err := NewWithConfig(conf) 77 | switch { 78 | case err != nil: 79 | t.Errorf("error on New: %s", err.Error()) 80 | case hook == nil: 81 | t.Errorf("hook should not be nil") 82 | case hook.conf.Host != testHOST: 83 | t.Errorf("hook.host should be %s, but %s", testHOST, hook.conf.Host) 84 | case hook.conf.Port != port: 85 | t.Errorf("hook.port should be %d, but %d", port, hook.conf.Port) 86 | case len(hook.levels) != len(defaultLevels): 87 | t.Errorf("hook.levels should be defaultLevels") 88 | case hook.Fluent == nil: 89 | t.Errorf("hook.Fluent should not be nil") 90 | case hook.messageField != "DefaultMessageField": 91 | t.Errorf("hook.messageField should be DefaultMessageField") 92 | case len(hook.ignoreFields) != len(conf.DefaultIgnoreFields): 93 | t.Errorf("hook.ignoreFields should be same as conf.DefaultIgnoreFields") 94 | case len(hook.filters) != len(conf.DefaultFilters): 95 | t.Errorf("hook.filters should be same as conf.DefaultFilters") 96 | } 97 | } 98 | 99 | func TestNewHook(t *testing.T) { 100 | const testPort = -1 101 | hook := NewHook(testHOST, testPort) 102 | switch { 103 | case hook == nil: 104 | t.Errorf("hook should not be nil") 105 | case hook.conf.Host != testHOST: 106 | t.Errorf("hook.host should be %s, but %s", testHOST, hook.conf.Host) 107 | case hook.conf.Port != testPort: 108 | t.Errorf("hook.port should be %d, but %d", testPort, hook.conf.Port) 109 | case len(hook.levels) != len(defaultLevels): 110 | t.Errorf("hook.levels should be defaultLevels") 111 | case hook.Fluent != nil: 112 | t.Errorf("hook.Fluent should be nil") 113 | case hook.messageField != MessageField: 114 | t.Errorf("hook.messageField should be %s", MessageField) 115 | } 116 | } 117 | 118 | func TestLevels(t *testing.T) { 119 | hook := FluentHook{} 120 | 121 | levels := hook.Levels() 122 | if levels != nil { 123 | t.Errorf("hook.Levels() should be nil, but %v", levels) 124 | } 125 | 126 | hook.levels = []logrus.Level{logrus.WarnLevel} 127 | levels = hook.Levels() 128 | switch { 129 | case levels == nil: 130 | t.Errorf("hook.Levels() should not be nil") 131 | case len(levels) != 1: 132 | t.Errorf("hook.Levels() should have 1 length") 133 | case levels[0] != logrus.WarnLevel: 134 | t.Errorf("hook.Levels() should have logrus.WarnLevel") 135 | } 136 | } 137 | 138 | func TestLevelWithCustomizers(t *testing.T) { 139 | a := assert.New(t) 140 | 141 | hook, err := NewWithConfig(Config{ 142 | Port: getOrCreateMockServer(t, data), 143 | Host: testHOST, 144 | }) 145 | a.NoError(err, "Error on NewWithConfig") 146 | 147 | hook.AddCustomizer(func(entry *logrus.Entry, data logrus.Fields) { 148 | data["level"] = "Hooked " + entry.Level.String() 149 | }) 150 | a.Len(hook.customizers, 1, "hook.customers has %d length, but %d", 1, len(hook.customizers)) 151 | 152 | fields := logrus.Fields{ 153 | "value": fieldValue, 154 | } 155 | 156 | const expected = "Hooked error" 157 | assertFunc := func(result string) { 158 | a.Contains(result, expected, fmt.Sprintf("actual %v should contain %v", result, expected)) 159 | } 160 | assertHook(t, hook, fields, "test message", assertFunc, data) 161 | } 162 | 163 | func TestSetLevels(t *testing.T) { 164 | hook := FluentHook{} 165 | 166 | levels := hook.levels 167 | if levels != nil { 168 | t.Errorf("hook.levels should be nil, but %v", levels) 169 | } 170 | 171 | hook.SetLevels([]logrus.Level{logrus.WarnLevel}) 172 | levels = hook.levels 173 | switch { 174 | case levels == nil: 175 | t.Errorf("hook.levels should not be nil") 176 | case len(levels) != 1: 177 | t.Errorf("hook.levels should have 1 length") 178 | case levels[0] != logrus.WarnLevel: 179 | t.Errorf("hook.levels should have logrus.WarnLevel") 180 | } 181 | 182 | hook.SetLevels(nil) 183 | levels = hook.levels 184 | if levels != nil { 185 | t.Errorf("hook.levels should be nil, but %v", levels) 186 | } 187 | } 188 | 189 | func TestTag(t *testing.T) { 190 | hook := FluentHook{} 191 | 192 | tag := hook.Tag() 193 | if tag != "" { 194 | t.Errorf("hook.Tag() should be empty, but %s", tag) 195 | } 196 | 197 | defaultTag := staticTag 198 | hook.tag = &defaultTag 199 | tag = hook.Tag() 200 | switch { 201 | case tag == "": 202 | t.Errorf("hook.Tag() should not be empty") 203 | case tag != defaultTag: 204 | t.Errorf("hook.Tag() should be %s, but %s", defaultTag, tag) 205 | } 206 | } 207 | 208 | func TestSetTag(t *testing.T) { 209 | hook := FluentHook{} 210 | 211 | tag := hook.tag 212 | if tag != nil { 213 | t.Errorf("hook.tag should be nil, but %s", *tag) 214 | } 215 | 216 | hook.SetTag(staticTag) 217 | tag = hook.tag 218 | switch { 219 | case tag == nil: 220 | t.Errorf("hook.tag should not be nil") 221 | case *tag != staticTag: 222 | t.Errorf("hook.tag should be %s, but %s", staticTag, *tag) 223 | } 224 | } 225 | 226 | func TestAddIgnore(t *testing.T) { 227 | hook := FluentHook{ 228 | ignoreFields: make(map[string]struct{}), 229 | } 230 | 231 | list := []string{"foo", "bar", "baz"} 232 | for i, key := range list { 233 | if len(hook.ignoreFields) != i { 234 | t.Errorf("hook.ignoreFields has %d length, but %d", i, len(hook.ignoreFields)) 235 | continue 236 | } 237 | 238 | hook.AddIgnore(key) 239 | if len(hook.ignoreFields) != i+1 { 240 | t.Errorf("hook.ignoreFields should be added") 241 | continue 242 | } 243 | for j := 0; j <= i; j++ { 244 | k := list[j] 245 | if _, ok := hook.ignoreFields[k]; !ok { 246 | t.Errorf("%s should be added into hook.ignoreFields", k) 247 | continue 248 | } 249 | } 250 | } 251 | } 252 | 253 | func TestAddFilter(t *testing.T) { 254 | hook := FluentHook{ 255 | filters: make(map[string]func(interface{}) interface{}), 256 | } 257 | 258 | list := []string{"foo", "bar", "baz"} 259 | for i, key := range list { 260 | if len(hook.filters) != i { 261 | t.Errorf("hook.filters has %d length, but %d", i, len(hook.filters)) 262 | continue 263 | } 264 | 265 | hook.AddFilter(key, nil) 266 | if len(hook.filters) != i+1 { 267 | t.Errorf("hook.filters should be added") 268 | continue 269 | } 270 | for j := 0; j <= i; j++ { 271 | k := list[j] 272 | if _, ok := hook.filters[k]; !ok { 273 | t.Errorf("%s should be added into hook.filters", k) 274 | continue 275 | } 276 | } 277 | } 278 | } 279 | 280 | func TestLogEntryMessageReceived(t *testing.T) { 281 | f := logrus.Fields{ 282 | "value": fieldValue, 283 | } 284 | 285 | assertion := func(result string) { 286 | switch { 287 | case !strings.Contains(result, assertEntryMessageAsFluentTag): 288 | t.Errorf("message should contain fluent-tag from entry.Message") 289 | case !strings.Contains(result, assertFieldValue): 290 | t.Errorf("message should contain value from field") 291 | } 292 | } 293 | assertLogHook(t, f, entryMessage, assertion) 294 | 295 | } 296 | 297 | func TestLogEntryMessageReceivedWithTag(t *testing.T) { 298 | f := logrus.Fields{ 299 | "tag": fieldTag, 300 | "value": fieldValue, 301 | } 302 | 303 | assertion := func(result string) { 304 | switch { 305 | case !strings.Contains(result, assertFieldTagAsFluentTag): 306 | t.Errorf("message should contain fluent-tag from field") 307 | case !strings.Contains(result, assertFieldValue): 308 | t.Errorf("message should contain value from field") 309 | case !strings.Contains(result, assertEntryMessage): 310 | t.Errorf("message should contain message from entry.Message") 311 | } 312 | } 313 | assertLogHook(t, f, entryMessage, assertion) 314 | } 315 | 316 | func TestLogEntryMessageReceivedWithCustomMessageField(t *testing.T) { 317 | conf := Config{ 318 | DefaultTag: fieldTag, 319 | DefaultMessageField: "my-message-field", 320 | } 321 | 322 | f := logrus.Fields{ 323 | "value": fieldValue, 324 | } 325 | 326 | assertion := func(result string) { 327 | switch { 328 | case !strings.Contains(result, assertFieldTagAsFluentTag): 329 | t.Errorf("message should contain fluent-tag from field") 330 | case !strings.Contains(result, assertFieldValue): 331 | t.Errorf("message should contain value from field") 332 | case !strings.Contains(result, "\xb0my-message-field\xaeMyEntryMessage"): 333 | t.Errorf("message should contain message from entry.Message") 334 | } 335 | } 336 | 337 | assertLogMessageWithConfig(t, conf, f, entryMessage, assertion) 338 | } 339 | 340 | func TestLogEntryMessageReceivedWithMessage(t *testing.T) { 341 | f := logrus.Fields{ 342 | "message": fieldMessage, 343 | "value": fieldValue, 344 | } 345 | 346 | assertion := func(result string) { 347 | switch { 348 | case !strings.Contains(result, assertEntryMessageAsFluentTag): 349 | t.Errorf("message should contain fluent-tag from entry.Message") 350 | case !strings.Contains(result, assertFieldValue): 351 | t.Errorf("message should contain value from field") 352 | case !strings.Contains(result, assertFieldMessage): 353 | t.Errorf("message should contain message from field") 354 | } 355 | } 356 | assertLogHook(t, f, entryMessage, assertion) 357 | } 358 | 359 | func TestLogEntryMessageReceivedWithTagAndMessage(t *testing.T) { 360 | f := logrus.Fields{ 361 | "message": fieldMessage, 362 | "tag": fieldTag, 363 | "value": fieldValue, 364 | } 365 | 366 | assertion := func(result string) { 367 | switch { 368 | case !strings.Contains(result, assertFieldTagAsFluentTag): 369 | t.Errorf("message should contain fluent-tag from field") 370 | case !strings.Contains(result, assertFieldValue): 371 | t.Errorf("message should contain value from field") 372 | case !strings.Contains(result, assertFieldMessage): 373 | t.Errorf("message should contain message from field") 374 | case strings.Contains(result, entryMessage): 375 | t.Errorf("message should not contain entry.Message") 376 | } 377 | } 378 | assertLogHook(t, f, entryMessage, assertion) 379 | } 380 | 381 | func TestLogEntryStaticTag(t *testing.T) { 382 | f := logrus.Fields{ 383 | "value": fieldValue, 384 | } 385 | 386 | assertion := func(result string) { 387 | switch { 388 | case !strings.Contains(result, assertStaticTagAsFluentTag): 389 | t.Errorf("message should contain fluent-tag from static tag") 390 | case !strings.Contains(result, assertFieldValue): 391 | t.Errorf("message should contain value from field") 392 | case !strings.Contains(result, assertEntryMessage): 393 | t.Errorf("message should contain message from entry.Message") 394 | } 395 | } 396 | assertLogHookWithStaticTag(t, f, entryMessage, assertion) 397 | } 398 | 399 | func TestLogEntryStaticTagWithTag(t *testing.T) { 400 | f := logrus.Fields{ 401 | "tag": fieldTag, 402 | "value": fieldValue, 403 | } 404 | 405 | assertion := func(result string) { 406 | switch { 407 | case !strings.Contains(result, assertStaticTagAsFluentTag): 408 | t.Errorf("message should contain fluent-tag from static tag") 409 | case !strings.Contains(result, assertFieldTag): 410 | t.Errorf("message should contain tag from field") 411 | case !strings.Contains(result, assertFieldValue): 412 | t.Errorf("message should contain value from field") 413 | case !strings.Contains(result, assertEntryMessage): 414 | t.Errorf("message should contain message from entry.Message") 415 | } 416 | } 417 | assertLogHookWithStaticTag(t, f, entryMessage, assertion) 418 | } 419 | 420 | func TestLogEntryStaticTagWithMessage(t *testing.T) { 421 | f := logrus.Fields{ 422 | "message": fieldMessage, 423 | "value": fieldValue, 424 | } 425 | 426 | assertion := func(result string) { 427 | switch { 428 | case !strings.Contains(result, assertStaticTagAsFluentTag): 429 | t.Errorf("message should contain fluent-tag from static tag") 430 | case !strings.Contains(result, assertFieldValue): 431 | t.Errorf("message should contain value from field") 432 | case strings.Contains(result, entryMessage): 433 | t.Errorf("message should not contain entry.Message") 434 | } 435 | } 436 | assertLogHookWithStaticTag(t, f, entryMessage, assertion) 437 | } 438 | 439 | func TestLogEntryStaticTagWithTagAndMessage(t *testing.T) { 440 | f := logrus.Fields{ 441 | "message": fieldMessage, 442 | "tag": fieldTag, 443 | "value": fieldValue, 444 | } 445 | 446 | assertion := func(result string) { 447 | switch { 448 | case !strings.Contains(result, assertStaticTagAsFluentTag): 449 | t.Errorf("message should contain fluent-tag from static tag") 450 | case !strings.Contains(result, assertFieldValue): 451 | t.Errorf("message should contain value from field") 452 | case !strings.Contains(result, assertFieldMessage): 453 | t.Errorf("message should contain message from field") 454 | case strings.Contains(result, entryMessage): 455 | t.Errorf("message should not contain entry.Message") 456 | } 457 | } 458 | assertLogHookWithStaticTag(t, f, entryMessage, assertion) 459 | } 460 | 461 | func assertLogHook(t *testing.T, f logrus.Fields, message string, assertFunc func(string)) { 462 | assertLogMessage(t, f, message, "", assertFunc) 463 | } 464 | 465 | func assertLogHookWithStaticTag(t *testing.T, f logrus.Fields, message string, assertFunc func(string)) { 466 | assertLogMessage(t, f, message, staticTag, assertFunc) 467 | } 468 | 469 | func assertLogMessage(t *testing.T, f logrus.Fields, message string, tag string, assertFunc func(string)) { 470 | // assert brand new logger 471 | { 472 | localData := make(chan string) 473 | _, port := newMockServer(t, localData) 474 | hook := NewHook(testHOST, port) 475 | if tag != "" { 476 | hook.SetTag(tag) 477 | } 478 | assertHook(t, hook, f, message, assertFunc, localData) 479 | } 480 | 481 | // assert persistent logger 482 | { 483 | port := getOrCreateMockServer(t, data) 484 | hook, err := New(testHOST, port) 485 | if err != nil { 486 | t.Errorf("Error on New: %s", err.Error()) 487 | } 488 | if tag != "" { 489 | hook.SetTag(tag) 490 | } 491 | assertHook(t, hook, f, message, assertFunc, data) 492 | } 493 | } 494 | 495 | func assertLogMessageWithConfig(t *testing.T, conf Config, f logrus.Fields, message string, assertFunc func(string)) { 496 | port := getOrCreateMockServer(t, data) 497 | conf.Port = port 498 | conf.Host = testHOST 499 | hook, err := NewWithConfig(conf) 500 | if err != nil { 501 | t.Errorf("Error on NewWithConfig: %s", err.Error()) 502 | } 503 | assertHook(t, hook, f, message, assertFunc, data) 504 | } 505 | 506 | func assertHook(t *testing.T, hook *FluentHook, f logrus.Fields, message string, assertFunc func(string), data chan string) { 507 | logger := logrus.New() 508 | logger.Hooks.Add(hook) 509 | 510 | for i := 0; i < defaultLoopCount; i++ { 511 | logger.WithFields(f).Error(message) 512 | assertFunc(<-data) 513 | } 514 | } 515 | 516 | func getOrCreateMockServer(t *testing.T, data chan string) int { 517 | if mockPort == 0 { 518 | _, mockPort = newMockServer(t, data) 519 | } 520 | return mockPort 521 | } 522 | 523 | func newMockServer(t *testing.T, data chan string) (net.Listener, int) { 524 | l, err := net.Listen("tcp", testHOST+":0") 525 | if err != nil { 526 | t.Errorf("Error listening: %s", err.Error()) 527 | } 528 | 529 | count := 0 530 | go func() { 531 | for { 532 | count++ 533 | conn, err := l.Accept() 534 | if err != nil { 535 | t.Errorf("Error accepting: %s", err.Error()) 536 | } 537 | 538 | go handleRequest(conn, l, data) 539 | if count == defaultLoopCount { 540 | l.Close() 541 | return 542 | } 543 | } 544 | }() 545 | return l, l.Addr().(*net.TCPAddr).Port 546 | } 547 | 548 | func handleRequest(conn net.Conn, l net.Listener, data chan string) { 549 | r := bufio.NewReader(conn) 550 | for { 551 | b := make([]byte, 1<<10) // Read 1KB at a time 552 | _, err := r.Read(b) 553 | if err == io.EOF { 554 | continue 555 | } else if err != nil { 556 | fmt.Printf("Error reading from connection: %s", err) 557 | } 558 | data <- string(b) 559 | } 560 | } 561 | -------------------------------------------------------------------------------- /reflect.go: -------------------------------------------------------------------------------- 1 | package logrus_fluent 2 | 3 | import ( 4 | "fmt" 5 | "reflect" 6 | "strings" 7 | ) 8 | 9 | // ConvertToValue make map data from struct and tags 10 | func ConvertToValue(p interface{}, tagName string) interface{} { 11 | rv := toValue(p) 12 | switch rv.Kind() { 13 | case reflect.Struct: 14 | if err, ok := p.(error); ok { 15 | return err.Error() 16 | } 17 | return convertFromStruct(rv.Interface(), tagName) 18 | case reflect.Map: 19 | return convertFromMap(rv, tagName) 20 | case reflect.Slice: 21 | return convertFromSlice(rv, tagName) 22 | case reflect.Chan: 23 | return nil 24 | case reflect.Invalid: 25 | return nil 26 | default: 27 | return rv.Interface() 28 | } 29 | } 30 | 31 | func convertFromMap(rv reflect.Value, tagName string) interface{} { 32 | result := make(map[string]interface{}) 33 | for _, key := range rv.MapKeys() { 34 | kv := rv.MapIndex(key) 35 | result[fmt.Sprint(key.Interface())] = ConvertToValue(kv.Interface(), tagName) 36 | } 37 | return result 38 | } 39 | 40 | func convertFromSlice(rv reflect.Value, tagName string) interface{} { 41 | var result []interface{} 42 | for i, max := 0, rv.Len(); i < max; i++ { 43 | result = append(result, ConvertToValue(rv.Index(i).Interface(), tagName)) 44 | } 45 | return result 46 | } 47 | 48 | // convertFromStruct converts struct to value 49 | // see: https://github.com/fatih/structs/ 50 | func convertFromStruct(p interface{}, tagName string) interface{} { 51 | result := make(map[string]interface{}) 52 | return convertFromStructDeep(result, tagName, toType(p), toValue(p)) 53 | } 54 | 55 | func convertFromStructDeep(result map[string]interface{}, tagName string, t reflect.Type, values reflect.Value) interface{} { 56 | for i, max := 0, t.NumField(); i < max; i++ { 57 | f := t.Field(i) 58 | if f.PkgPath != "" && !f.Anonymous { 59 | continue 60 | } 61 | 62 | if f.Anonymous { 63 | tt := f.Type 64 | if tt.Kind() == reflect.Ptr { 65 | tt = tt.Elem() 66 | } 67 | vv := values.Field(i) 68 | if !vv.IsValid() { 69 | continue 70 | } 71 | if vv.Kind() == reflect.Ptr { 72 | vv = vv.Elem() 73 | } 74 | 75 | if vv.Kind() == reflect.Struct { 76 | convertFromStructDeep(result, tagName, tt, vv) 77 | } 78 | continue 79 | } 80 | 81 | tag, opts := parseTag(f, tagName) 82 | if tag == "-" { 83 | continue // skip `-` tag 84 | } 85 | 86 | if !values.IsValid() { 87 | continue 88 | } 89 | v := values.Field(i) 90 | if opts.Has("omitempty") && isZero(v) { 91 | continue // skip zero-value when omitempty option exists in tag 92 | } 93 | name := getNameFromTag(f, tagName) 94 | result[name] = ConvertToValue(v.Interface(), TagName) 95 | } 96 | return result 97 | } 98 | 99 | // toValue converts any value to reflect.Value 100 | func toValue(p interface{}) reflect.Value { 101 | v := reflect.ValueOf(p) 102 | if v.Kind() == reflect.Ptr { 103 | v = v.Elem() 104 | } 105 | return v 106 | } 107 | 108 | // toType converts any value to reflect.Type 109 | func toType(p interface{}) reflect.Type { 110 | t := reflect.ValueOf(p).Type() 111 | if t.Kind() == reflect.Ptr { 112 | t = t.Elem() 113 | } 114 | return t 115 | } 116 | 117 | // isZero checks the value is zero-value or not 118 | func isZero(v reflect.Value) bool { 119 | zero := reflect.Zero(v.Type()).Interface() 120 | value := v.Interface() 121 | return reflect.DeepEqual(value, zero) 122 | } 123 | 124 | // getNameFromTag return the value in tag or field name in the struct field 125 | func getNameFromTag(f reflect.StructField, tagName string) string { 126 | tag, _ := parseTag(f, tagName) 127 | if tag != "" { 128 | return tag 129 | } 130 | return f.Name 131 | } 132 | 133 | // getTagValues returns tag value of the struct field 134 | func getTagValues(f reflect.StructField, tag string) string { 135 | return f.Tag.Get(tag) 136 | } 137 | 138 | // parseTag returns the first tag value of the struct field 139 | func parseTag(f reflect.StructField, tag string) (string, options) { 140 | return splitTags(getTagValues(f, tag)) 141 | } 142 | 143 | // splitTags returns the first tag value and rest slice 144 | func splitTags(tags string) (string, options) { 145 | res := strings.Split(tags, ",") 146 | return res[0], res[1:] 147 | } 148 | 149 | // TagOptions is wrapper struct for rest tag values 150 | type options []string 151 | 152 | // Has checks the value exists in the rest values or not 153 | func (t options) Has(tag string) bool { 154 | for _, opt := range t { 155 | if opt == tag { 156 | return true 157 | } 158 | } 159 | return false 160 | } 161 | -------------------------------------------------------------------------------- /reflect_test.go: -------------------------------------------------------------------------------- 1 | package logrus_fluent 2 | 3 | import ( 4 | "errors" 5 | "testing" 6 | 7 | "github.com/stretchr/testify/assert" 8 | ) 9 | 10 | type eyes int 11 | 12 | type animal struct { 13 | eyes 14 | 15 | Fur bool 16 | skills string 17 | } 18 | 19 | type Creature struct { 20 | *animal 21 | Name string 22 | Human bool 23 | Height int 24 | Weight int 25 | Alias string `fluent:"nickname"` 26 | Cute bool `fluent:"-"` 27 | } 28 | 29 | func TestConvertToValuError(t *testing.T) { 30 | assert := assert.New(t) 31 | 32 | err := errors.New("the error") 33 | data := map[string]interface{}{"error": err} 34 | 35 | result := ConvertToValue(data, TagName) 36 | 37 | r, ok := result.(map[string]interface{}) 38 | assert.True(ok) 39 | assert.Equal(err.Error(), r["error"]) 40 | } 41 | 42 | func TestConvertToValueStruct(t *testing.T) { 43 | assert := assert.New(t) 44 | 45 | v := Creature{ 46 | animal: &animal{ 47 | eyes: 2, 48 | Fur: true, 49 | skills: "kill,purr", 50 | }, 51 | Name: "cat", 52 | Height: 50, 53 | Weight: 4, 54 | Alias: "tama", 55 | } 56 | result := ConvertToValue(v, TagName) 57 | 58 | r, ok := result.(map[string]interface{}) 59 | assert.True(ok) 60 | assert.Equal(v.Name, r["Name"]) 61 | assert.Equal(v.Height, r["Height"]) 62 | assert.Equal(v.Weight, r["Weight"]) 63 | assert.Equal(v.Alias, r["nickname"]) 64 | assert.Equal(v.Human, r["Human"]) 65 | assert.Equal(v.Fur, r["Fur"]) 66 | assert.NotContains(r, "Cute") 67 | assert.NotContains(r, "skills") 68 | assert.NotContains(r, "eyes") 69 | } 70 | 71 | func TestConvertToValueSlice(t *testing.T) { 72 | assert := assert.New(t) 73 | 74 | var list []*Creature 75 | list = append(list, &Creature{Name: "cat"}) 76 | list = append(list, &Creature{Name: "dog"}) 77 | list = append(list, nil) 78 | list = append(list, &Creature{Name: "bird"}) 79 | 80 | result := ConvertToValue(list, TagName) 81 | r, ok := result.([]interface{}) 82 | assert.True(ok) 83 | assert.Len(r, 4) 84 | } 85 | 86 | func TestConvertToValueNil(t *testing.T) { 87 | assert := assert.New(t) 88 | result := ConvertToValue(nil, TagName) 89 | assert.Equal(nil, result) 90 | 91 | var ptr *Creature 92 | result = ConvertToValue(ptr, TagName) 93 | assert.Equal(nil, result) 94 | } 95 | -------------------------------------------------------------------------------- /wercker.yml: -------------------------------------------------------------------------------- 1 | box: golang:1.12.7-stretch 2 | dev: 3 | build: 4 | steps: 5 | - setup-go-workspace 6 | - script: 7 | name: go get 8 | code: | 9 | go get -t -v ./... 10 | - script: 11 | name: lint 12 | code: | 13 | make lint 14 | - script: 15 | name: test 16 | code: | 17 | make test 18 | --------------------------------------------------------------------------------