View Javadoc
1   /*
2    * Copyright (c) 2001-2017, Zoltan Farkas All Rights Reserved.
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 2.1 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this program; if not, write to the Free Software
16   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17   *
18   * Additionally licensed with:
19   *
20   * Licensed under the Apache License, Version 2.0 (the "License");
21   * you may not use this file except in compliance with the License.
22   * You may obtain a copy of the License at
23   *
24   *      http://www.apache.org/licenses/LICENSE-2.0
25   *
26   * Unless required by applicable law or agreed to in writing, software
27   * distributed under the License is distributed on an "AS IS" BASIS,
28   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29   * See the License for the specific language governing permissions and
30   * limitations under the License.
31   */
32  package org.spf4j.log;
33  
34  import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
35  import java.time.Instant;
36  import java.util.Collections;
37  import java.util.Set;
38  import javax.annotation.Nullable;
39  import org.slf4j.Marker;
40  import org.spf4j.base.avro.Converters;
41  import org.spf4j.base.avro.LogRecord;
42  
43  /**
44   *
45   * @author Zoltan Farkas
46   */
47  public final class AvroLogRecordImpl implements Slf4jLogRecord {
48  
49    private final LogRecord record;
50  
51    private boolean isLogged;
52  
53    public AvroLogRecordImpl(final LogRecord record) {
54      this(record, false);
55    }
56  
57    @SuppressFBWarnings("EI_EXPOSE_REP2")
58    public AvroLogRecordImpl(final LogRecord record, final boolean isLogged) {
59      this.record = record;
60      this.isLogged = isLogged;
61    }
62  
63    @Override
64    public LogRecord toLogRecord(final String origin, final String traceId) {
65      return LogRecord.newBuilder(record).setOrigin(origin).setTrId(traceId).build();
66    }
67  
68    @Override
69    public Object[] getArguments() {
70      return new Object[] {record};
71    }
72  
73    @Override
74    public Object[] getExtraArgumentsRaw() {
75      return getArguments();
76    }
77  
78    @Override
79    public Object[] getExtraArguments() {
80      return getArguments();
81    }
82  
83    @Override
84    public Throwable getExtraThrowable() {
85      return Converters.convert(record.getOrigin(), record.getThrowable());
86    }
87  
88    @Override
89    public Level getLevel() {
90      return Level.fromAvroLevel(record.getLevel());
91    }
92  
93    @Override
94    public String getLoggerName() {
95      return record.getLogger();
96    }
97  
98    @Override
99    @Nullable
100   public Marker getMarker() {
101     return null;
102   }
103 
104   @Override
105   public String getMessage() {
106     return "RemoteLog";
107   }
108 
109   @Override
110   public String getMessageFormat() {
111     return getMessage();
112   }
113 
114   @Override
115   public int getNrMessageArguments() {
116     return 0;
117   }
118 
119   @Override
120   public String getThreadName() {
121     return record.getThr();
122   }
123 
124   @Override
125   public long getTimeStamp() {
126     return record.getTs().toEpochMilli();
127   }
128 
129   @Override
130   public Instant getTimeStampInstant() {
131     return record.getTs();
132   }
133 
134   @Override
135   public synchronized boolean isLogged() {
136     return isLogged;
137   }
138 
139   @Override
140   public synchronized void setIsLogged() {
141     this.isLogged = true;
142   }
143 
144   @Override
145   public void attach(final Object obj) {
146     // do not attach;
147   }
148 
149   @Override
150   public Set<Object> getAttachments() {
151     return Collections.EMPTY_SET;
152   }
153 
154   @Override
155   public boolean hasAttachment(final Object obj) {
156     return false;
157   }
158 
159   @Override
160   public String toString() {
161     return "AvroLogRecordImpl{" + "record=" + record + ", isLogged=" + isLogged + '}';
162   }
163 
164 }