github.heygears.com/openimsdk/tools@v0.0.49/log/encoder.go (about) 1 // Copyright © 2024 OpenIM open source community. All rights reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package log 16 17 import ( 18 "fmt" 19 20 "go.uber.org/zap/buffer" 21 "go.uber.org/zap/zapcore" 22 ) 23 24 type alignEncoder struct { 25 zapcore.Encoder 26 } 27 28 // EncodeEntry is a custom method to wrap the original EncodeEntry and align the message. 29 func (ae *alignEncoder) EncodeEntry(entry zapcore.Entry, fields []zapcore.Field) (*buffer.Buffer, error) { 30 // Here we can manipulate the entry.Message to align it as we want 31 // For left alignment, you might want to pad it with spaces to a certain width 32 entry.Message = fmt.Sprintf("%-50s", entry.Message) // Left align and pad to 50 characters 33 34 // Call the original Encoder's EncodeEntry method with the modified entry. 35 return ae.Encoder.EncodeEntry(entry, fields) 36 }