This repository was archived by the owner on Sep 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathVmRuntimeFileLogHandler.java
More file actions
149 lines (131 loc) · 5 KB
/
VmRuntimeFileLogHandler.java
File metadata and controls
149 lines (131 loc) · 5 KB
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
/*
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.apphosting.vmruntime;
import com.google.apphosting.logging.JsonFormatter;
import java.io.IOException;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.logging.FileHandler;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
/**
* {@code VmRuntimeFileLogHandler} is installed on the root logger. It converts all messages
* to the json format understood by the cloud logging agent and logs to a file in a volume shared
* with the cloud logging agent.
*/
public class VmRuntimeFileLogHandler extends FileHandler {
// This exists for testing purposes only. If set, the cloud logger may lose logs.
public static final String LOG_DIRECTORY_PROPERTY = "com.google.apphosting.logs";
public static final String LOG_PATTERN_CONFIG_PROPERTY =
"com.google.apphosting.vmruntime.VmRuntimeFileLogHandler.pattern";
// Log files to /var/log/app_engine/app.[0-2].log.json
private static final String DEFAULT_LOG_DIRECTORY = "/var/log/app_engine";
private static final String DEFAULT_LOG_PATTERN = "app.%g.log.json";
private static final String APP_ENGINE_LOG_CONFIG_PATTERN_ENV = "APP_ENGINE_LOG_CONFIG_PATTERN";
private static final int LOG_MAX_SIZE = 100 * 1024 * 1024;
private static final int LOG_MAX_FILES = 3;
public static final String JAVA_UTIL_LOGGING_CONFIG_PROPERTY = "java.util.logging.config.file";
private static final LogRecord CLOSE_RECORD = new LogRecord(Level.ALL, "CLOSE");
private final BlockingQueue<LogRecord> queue = new LinkedBlockingQueue<>();
private VmRuntimeFileLogHandler() throws IOException {
super(fileLogPattern(), LOG_MAX_SIZE, LOG_MAX_FILES, true);
setLevel(Level.FINEST);
setFormatter(new JsonFormatter());
new LoggerThread().start();
}
private static String fileLogPattern() {
String pattern = System.getenv(APP_ENGINE_LOG_CONFIG_PATTERN_ENV);
// For Cloud SDK usage only for local Jetty processes.
if (pattern != null) {
return pattern;
}
String directory = System.getProperty(LOG_DIRECTORY_PROPERTY, DEFAULT_LOG_DIRECTORY);
pattern = System.getProperty(LOG_PATTERN_CONFIG_PROPERTY);
if (pattern != null) {
if (pattern.startsWith("/")) {
return pattern;
}
return directory + "/" + pattern;
}
return directory + "/" + DEFAULT_LOG_PATTERN;
}
/**
* Initialize the {@code VmRuntimeFileLogHandler} by installing it on the root logger.
*/
public static void init() throws IOException {
reloadLoggingProperties(LogManager.getLogManager());
Logger rootLogger = Logger.getLogger("");
for (Handler handler : rootLogger.getHandlers()) {
System.out.println("Logging Handler Found: " + handler.getClass().getCanonicalName());
if (handler instanceof VmRuntimeFileLogHandler) {
return; // Already installed.
}
}
System.out.println("Adding VmRuntimeFileLogHandler");
rootLogger.addHandler(new VmRuntimeFileLogHandler());
}
/**
* Reloads logging to pick up changes to the java.util.logging.config.file system property.
*/
private static void reloadLoggingProperties(LogManager logManager) {
String logging = System.getProperty(VmRuntimeFileLogHandler.JAVA_UTIL_LOGGING_CONFIG_PROPERTY);
if (logging == null) {
return;
}
try {
logManager.readConfiguration();
} catch (SecurityException | IOException e) {
e.printStackTrace();
System.err.println("Warning: caught exception when reading logging properties.");
System.err.println(e.getClass().getName() + ": " + e.getMessage());
}
}
@Override
public void close() throws SecurityException {
super.close();
queue.offer(CLOSE_RECORD);
}
@Override
public void publish(LogRecord record) {
if (isLoggable(record)) {
queue.offer(record);
}
}
private void superPublish(LogRecord record) {
super.publish(record);
}
class LoggerThread extends Thread {
@Override
public void run() {
while (true) {
try {
LogRecord record = queue.poll(1, TimeUnit.HOURS);
if (record == CLOSE_RECORD) {
return;
} else {
superPublish(record);
}
} catch (InterruptedException e) {
return;
}
}
}
}
}