-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
StoreLogService.java
245 lines (216 loc) · 8.61 KB
/
StoreLogService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*
* Copyright (c) 2002-2018 "Neo4j,"
* Neo4j Sweden AB [http://neo4j.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.kernel.impl.logging;
import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executor;
import java.util.function.Consumer;
import org.neo4j.io.fs.FileSystemAbstraction;
import org.neo4j.kernel.lifecycle.Lifecycle;
import org.neo4j.logging.FormattedLogProvider;
import org.neo4j.logging.Level;
import org.neo4j.logging.LogProvider;
import org.neo4j.logging.NullLogProvider;
import org.neo4j.logging.RotatingFileOutputStreamSupplier;
import org.neo4j.scheduler.Group;
import org.neo4j.scheduler.JobScheduler;
import static org.neo4j.io.file.Files.createOrOpenAsOutputStream;
public class StoreLogService extends AbstractLogService implements Lifecycle
{
public static class Builder
{
private LogProvider userLogProvider = NullLogProvider.getInstance();
private Executor rotationExecutor;
private long internalLogRotationThreshold;
private long internalLogRotationDelay;
private int maxInternalLogArchives;
private Consumer<LogProvider> rotationListener = logProvider ->
{
};
private Map<String, Level> logLevels = new HashMap<>();
private Level defaultLevel = Level.INFO;
private ZoneId timeZoneId = ZoneOffset.UTC;
private File debugLog;
private Builder()
{
}
public Builder withUserLogProvider( LogProvider userLogProvider )
{
this.userLogProvider = userLogProvider;
return this;
}
public Builder withRotation( long internalLogRotationThreshold, long internalLogRotationDelay,
int maxInternalLogArchives, JobScheduler jobScheduler )
{
return withRotation( internalLogRotationThreshold, internalLogRotationDelay, maxInternalLogArchives,
jobScheduler.executor( Group.LOG_ROTATION ) );
}
public Builder withRotation( long internalLogRotationThreshold, long internalLogRotationDelay,
int maxInternalLogArchives, Executor rotationExecutor )
{
this.internalLogRotationThreshold = internalLogRotationThreshold;
this.internalLogRotationDelay = internalLogRotationDelay;
this.maxInternalLogArchives = maxInternalLogArchives;
this.rotationExecutor = rotationExecutor;
return this;
}
public Builder withRotationListener( Consumer<LogProvider> rotationListener )
{
this.rotationListener = rotationListener;
return this;
}
public Builder withLevel( String context, Level level )
{
this.logLevels.put( context, level );
return this;
}
public Builder withTimeZone( ZoneId timeZoneId )
{
this.timeZoneId = timeZoneId;
return this;
}
public Builder withDefaultLevel( Level defaultLevel )
{
this.defaultLevel = defaultLevel;
return this;
}
public Builder withInternalLog( File logFile )
{
this.debugLog = logFile;
return this;
}
public StoreLogService build( FileSystemAbstraction fileSystem ) throws IOException
{
if ( debugLog == null )
{
throw new IllegalArgumentException( "Debug log can't be null; set its value using `withInternalLog`" );
}
return new StoreLogService( userLogProvider, fileSystem, debugLog, logLevels, defaultLevel, timeZoneId,
internalLogRotationThreshold, internalLogRotationDelay, maxInternalLogArchives, rotationExecutor,
rotationListener );
}
}
public static Builder withUserLogProvider( LogProvider userLogProvider )
{
return new Builder().withUserLogProvider( userLogProvider );
}
public static Builder withRotation( long internalLogRotationThreshold, long internalLogRotationDelay,
int maxInternalLogArchives, JobScheduler jobScheduler )
{
return new Builder().withRotation( internalLogRotationThreshold, internalLogRotationDelay, maxInternalLogArchives,
jobScheduler );
}
public static Builder withInternalLog( File logFile )
{
return new Builder().withInternalLog( logFile );
}
private final Closeable closeable;
private final SimpleLogService logService;
private StoreLogService( LogProvider userLogProvider,
FileSystemAbstraction fileSystem,
File internalLog,
Map<String, Level> logLevels,
Level defaultLevel,
ZoneId logTimeZone,
long internalLogRotationThreshold,
long internalLogRotationDelay,
int maxInternalLogArchives,
Executor rotationExecutor,
final Consumer<LogProvider> rotationListener ) throws IOException
{
if ( !internalLog.getParentFile().exists() )
{
fileSystem.mkdirs( internalLog.getParentFile() );
}
final FormattedLogProvider.Builder internalLogBuilder = FormattedLogProvider.withZoneId( logTimeZone )
.withDefaultLogLevel( defaultLevel ).withLogLevels( logLevels );
FormattedLogProvider internalLogProvider;
if ( internalLogRotationThreshold == 0 )
{
OutputStream outputStream = createOrOpenAsOutputStream( fileSystem, internalLog, true );
internalLogProvider = internalLogBuilder.toOutputStream( outputStream );
rotationListener.accept( internalLogProvider );
this.closeable = outputStream;
}
else
{
RotatingFileOutputStreamSupplier rotatingSupplier = new RotatingFileOutputStreamSupplier( fileSystem, internalLog,
internalLogRotationThreshold, internalLogRotationDelay, maxInternalLogArchives,
rotationExecutor, new RotatingFileOutputStreamSupplier.RotationListener()
{
@Override
public void outputFileCreated( OutputStream newStream )
{
FormattedLogProvider logProvider = internalLogBuilder.toOutputStream( newStream );
logProvider.getLog( StoreLogService.class ).info( "Opened new internal log file" );
rotationListener.accept( logProvider );
}
@Override
public void rotationCompleted( OutputStream newStream )
{
FormattedLogProvider logProvider = internalLogBuilder.toOutputStream( newStream );
logProvider.getLog( StoreLogService.class ).info( "Rotated internal log file" );
}
@Override
public void rotationError( Exception e, OutputStream outStream )
{
FormattedLogProvider logProvider = internalLogBuilder.toOutputStream( outStream );
logProvider.getLog( StoreLogService.class ).info( "Rotation of internal log file failed:", e );
}
} );
internalLogProvider = internalLogBuilder.toOutputStream( rotatingSupplier );
this.closeable = rotatingSupplier;
}
this.logService = new SimpleLogService( userLogProvider, internalLogProvider );
}
@Override
public void init()
{
}
@Override
public void start()
{
}
@Override
public void stop()
{
}
@Override
public void shutdown() throws Throwable
{
closeable.close();
}
@Override
public LogProvider getUserLogProvider()
{
return logService.getUserLogProvider();
}
@Override
public LogProvider getInternalLogProvider()
{
return logService.getInternalLogProvider();
}
}