shared/log15/format.go
func JsonFormatEx(pretty, lineSeparated bool) Format {
	jsonMarshal := json.Marshal
	if pretty {
		jsonMarshal = func(v interface{}) ([]byte, error) {
			return json.MarshalIndent(v, "", "    ")
		}
	}

	return FormatFunc(func(r *Record) []byte {
		props := make(map[string]interface{})

		props[r.KeyNames.Time] = r.Time
		props[r.KeyNames.Lvl] = r.Lvl
		props[r.KeyNames.Msg] = r.Msg

		for i := 0; i < len(r.Ctx); i += 2 {
			k, ok := r.Ctx[i].(string)
			if !ok {
				props[errorKey] = fmt.Sprintf("%+v is not a string key", r.Ctx[i])
			}
			props[k] = formatJsonValue(r.Ctx[i+1])
		}

		b, err := jsonMarshal(props)
		if err != nil {
			b, _ = jsonMarshal(map[string]string{
				errorKey: err.Error(),
			})
			return b
		}

		if lineSeparated {
			b = append(b, '\n')
		}

		return b
	})
}
func JsonFormatEx(pretty, lineSeparated bool) Format {
	jsonMarshal := json.Marshal
	if pretty {
		jsonMarshal = func(v interface{}) ([]byte, error) {
			return json.MarshalIndent(v, "", "    ")
		}
	}

	return FormatFunc(func(r *Record) []byte {
		props := make(map[string]interface{})

		props[r.KeyNames.Time] = r.Time
		props[r.KeyNames.Lvl] = r.Lvl
		props[r.KeyNames.Msg] = r.Msg

		for i := 0; i < len(r.Ctx); i += 2 {
			k, ok := r.Ctx[i].(string)
			if !ok {
				props[errorKey] = fmt.Sprintf("%+v is not a string key", r.Ctx[i])
			}
			props[k] = formatJsonValue(r.Ctx[i+1])
		}

		b, err := jsonMarshal(props)
		if err != nil {
			b, _ = jsonMarshal(map[string]string{
				errorKey: err.Error(),
			})
			return b
		}

		if lineSeparated {
			b = append(b, '\n')
		}

		return b
	})
}
func JsonFormatEx ( pretty , lineSeparated bool ) Format { jsonMarshal := json . Marshal if pretty { jsonMarshal = func ( v interface { } ) ( [ ] byte , error ) { return json . MarshalIndent ( v , " " , " " ) } } return FormatFunc ( func ( r * Record ) [ ] byte { props := make ( map [ string ] interface { } ) props [ r . KeyNames . Time ] = r . Time props [ r . KeyNames . Lvl ] = r . Lvl props [ r . KeyNames . Msg ] = r . Msg for i := 0 ; i < len ( r . Ctx ) ; i += 2 { k , ok := r . Ctx [ i ] . ( string ) if ! ok { props [ errorKey ] = fmt . Sprintf ( " " , r . Ctx [ i ] ) } props [ k ] = formatJsonValue ( r . Ctx [ i + 1 ] ) } b , err := jsonMarshal ( props ) if err != nil { b , _ = jsonMarshal ( map [ string ] string { errorKey : err . Error ( ) , } ) return b } if lineSeparated { b = append ( b , '\n' ) } return b } ) }
// JsonFormatEx formats log records as JSON objects. If pretty is true,
// records will be pretty-printed. If lineSeparated is true, records
// will be logged with a new line between each record.
JsonFormatEx formats log records as JSON objects . If pretty is true records will be pretty - printed . If lineSeparated is true records will be logged with a new line between each record .