Skip to content

Commit 5046996

Browse files
thomaspoignantThomas Poignant
and
Thomas Poignant
authored
Deprecate Format and use LogFormat instead (#244)
Co-authored-by: Thomas Poignant <[email protected]>
1 parent 0830963 commit 5046996

File tree

3 files changed

+24
-13
lines changed

3 files changed

+24
-13
lines changed

docs/data_collection/log.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@ ffclient.Config{
1111
// ...
1212
DataExporter: ffclient.DataExporter{
1313
Exporter: &ffexporter.Log{
14-
Format: "[{{ .FormattedDate}}] user=\"{{ .UserKey}}\", flag=\"{{ .Key}}\", value=\"{{ .Value}}\"",
14+
LogFormat: "[{{ .FormattedDate}}] user=\"{{ .UserKey}}\", flag=\"{{ .Key}}\", value=\"{{ .Value}}\"",
1515
},
1616
},
1717
// ...
1818
}
1919
```
2020

2121
## Configuration fields
22-
| Field | Description |
23-
|---|---|
24-
|`Format` | *(optional)*<br>Format is the [template](https://golang.org/pkg/text/template/) configuration of the output format of your log.<br>You can use all the key from the `exporter.FeatureEvent` + a key called `FormattedDate` that represent the date with the **RFC 3339** Format.<br><br>**Default: `[{{ .FormattedDate}}] user="{{ .UserKey}}", flag="{{ .Key}}", value="{{ .Value}}"`** |
22+
| Field | Description |
23+
|-------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
24+
| `LogFormat` | *(optional)*<br>LogFormat is the [template](https://golang.org/pkg/text/template/) configuration of the output format of your log.<br>You can use all the key from the `exporter.FeatureEvent` + a key called `FormattedDate` that represent the date with the **RFC 3339** Format.<br><br>**Default: `[{{ .FormattedDate}}] user="{{ .UserKey}}", flag="{{ .Key}}", value="{{ .Value}}"`** |
2525

2626
Check the [godoc for full details](https://pkg.go.dev/github.com/thomaspoignant/[email protected]/ffexporter#Log).

ffexporter/log.go

+13-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,13 @@ type Log struct {
1616
// You can use all the key from the exporter.FeatureEvent + a key called FormattedDate that represent the date with
1717
// the RFC 3339 Format
1818
// Default: [{{ .FormattedDate}}] user="{{ .UserKey}}", flag="{{ .Key}}", value="{{ .Value}}"
19-
Format string
19+
Format string // Deprecated: use LogFormat instead.
20+
21+
// Format is the template configuration of the output format of your log.
22+
// You can use all the key from the exporter.FeatureEvent + a key called FormattedDate that represent the date with
23+
// the RFC 3339 Format
24+
// Default: [{{ .FormattedDate}}] user="{{ .UserKey}}", flag="{{ .Key}}", value="{{ .Value}}"
25+
LogFormat string
2026

2127
logTemplate *template.Template
2228
initTemplates sync.Once
@@ -25,7 +31,12 @@ type Log struct {
2531
// Export is saving a collection of events in a file.
2632
func (f *Log) Export(ctx context.Context, logger *log.Logger, featureEvents []FeatureEvent) error {
2733
f.initTemplates.Do(func() {
28-
f.logTemplate = parseTemplate("logFormat", f.Format, defaultLoggerFormat)
34+
// Remove bellow after deprecation of Format
35+
if f.LogFormat == "" && f.Format != "" {
36+
f.LogFormat = f.Format
37+
}
38+
39+
f.logTemplate = parseTemplate("logFormat", f.LogFormat, defaultLoggerFormat)
2940
})
3041

3142
for _, event := range featureEvents {

ffexporter/log_test.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313

1414
func TestLog_Export(t *testing.T) {
1515
type fields struct {
16-
Format string
16+
LogFormat string
1717
}
1818
type args struct {
1919
featureEvents []ffexporter.FeatureEvent
@@ -27,7 +27,7 @@ func TestLog_Export(t *testing.T) {
2727
}{
2828
{
2929
name: "Default format",
30-
fields: fields{Format: ""},
30+
fields: fields{LogFormat: ""},
3131
args: args{featureEvents: []ffexporter.FeatureEvent{
3232
{Kind: "feature", ContextKind: "anonymousUser", UserKey: "ABCD", CreationDate: 1617970547, Key: "random-key",
3333
Variation: "Default", Value: "YO", Default: false},
@@ -37,7 +37,7 @@ func TestLog_Export(t *testing.T) {
3737
{
3838
name: "Custom format",
3939
fields: fields{
40-
Format: "key=\"{{ .Key}}\" [{{ .FormattedDate}}]",
40+
LogFormat: "key=\"{{ .Key}}\" [{{ .FormattedDate}}]",
4141
},
4242
args: args{featureEvents: []ffexporter.FeatureEvent{
4343
{Kind: "feature", ContextKind: "anonymousUser", UserKey: "ABCD", CreationDate: 1617970547, Key: "random-key",
@@ -46,9 +46,9 @@ func TestLog_Export(t *testing.T) {
4646
expectedLog: "key=\"random-key\" \\[" + testutils.RFC3339Regex + "\\]\n",
4747
},
4848
{
49-
name: "Format error",
49+
name: "LogFormat error",
5050
fields: fields{
51-
Format: "key=\"{{ .Key}\" [{{ .FormattedDate}}]",
51+
LogFormat: "key=\"{{ .Key}\" [{{ .FormattedDate}}]",
5252
},
5353
args: args{featureEvents: []ffexporter.FeatureEvent{
5454
{Kind: "feature", ContextKind: "anonymousUser", UserKey: "ABCD", CreationDate: 1617970547, Key: "random-key",
@@ -59,7 +59,7 @@ func TestLog_Export(t *testing.T) {
5959
{
6060
name: "Field does not exist",
6161
fields: fields{
62-
Format: "key=\"{{ .UnknownKey}}\" [{{ .FormattedDate}}]",
62+
LogFormat: "key=\"{{ .UnknownKey}}\" [{{ .FormattedDate}}]",
6363
},
6464
args: args{featureEvents: []ffexporter.FeatureEvent{
6565
{Kind: "feature", ContextKind: "anonymousUser", UserKey: "ABCD", CreationDate: 1617970547, Key: "random-key",
@@ -72,7 +72,7 @@ func TestLog_Export(t *testing.T) {
7272
for _, tt := range tests {
7373
t.Run(tt.name, func(t *testing.T) {
7474
f := &ffexporter.Log{
75-
Format: tt.fields.Format,
75+
LogFormat: tt.fields.LogFormat,
7676
}
7777

7878
logFile, _ := ioutil.TempFile("", "")

0 commit comments

Comments
 (0)