This repository has been archived by the owner on Sep 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #295 from jetty-project/bugs/231-julroot
Issue #231 - Logging Rotation and Levels
- Loading branch information
Showing
25 changed files
with
806 additions
and
444 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
149 changes: 149 additions & 0 deletions
149
appengine-java-logging/src/main/java/com/google/apphosting/logging/CoreLogging.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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.logging; | ||
|
||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.util.ServiceLoader; | ||
import java.util.logging.LogManager; | ||
|
||
/** | ||
* Logging Core behavior. | ||
*/ | ||
public class CoreLogging { | ||
public static final String JAVA_UTIL_LOGGING_CONFIG_PROPERTY = "java.util.logging.config.file"; | ||
private static final boolean DEBUG = Boolean.getBoolean("com.google.apphosting.logging.DEBUG"); | ||
|
||
/** | ||
* Initialize the java.util.logging Environment. | ||
* <p> | ||
* Order is: | ||
* <ol> | ||
* <li>Apply App (User) Configuration</li> | ||
* <li>Apply System Mandated Configuration</li> | ||
* </ol> | ||
* </p> | ||
* | ||
* @param appConfigFile the filename for the (optional) app specific java.util.logging | ||
* properties file configuration. | ||
*/ | ||
public static void init(File appConfigFile) throws IOException { | ||
// Use App (User) Configuration specified as a file parameter | ||
if (appConfigFile != null && appConfigFile.exists()) { | ||
debug("Loading App Config (from file): %s", appConfigFile); | ||
appConfig(appConfigFile); | ||
} else { | ||
// Use App (User) Configuration specified as a System property | ||
String julConfigFile = System.getProperty(JAVA_UTIL_LOGGING_CONFIG_PROPERTY); | ||
if (julConfigFile != null) { | ||
File configFile = new File(julConfigFile); | ||
if (configFile.exists()) { | ||
debug("Loading App Config (from property): %s", appConfigFile); | ||
appConfig(configFile); | ||
} else { | ||
warning("Config System Property (%s) points to invalid file: %s", | ||
JAVA_UTIL_LOGGING_CONFIG_PROPERTY, appConfigFile.getAbsolutePath()); | ||
} | ||
} else { | ||
debug("No App Config"); | ||
} | ||
} | ||
|
||
// Manually Adjust Configuration to support System Logging Requirements | ||
systemConfig(); | ||
} | ||
|
||
/** | ||
* Convenience method for {@link #init(File)}. | ||
* | ||
* @param appConfigFilename the filename of the config file (or null) | ||
* @throws IOException if unable to configure the logging | ||
*/ | ||
public static void init(String appConfigFilename) throws IOException { | ||
File appConfigFile = null; | ||
if (appConfigFilename != null) { | ||
appConfigFile = new File(appConfigFilename); | ||
} | ||
init(appConfigFile); | ||
} | ||
|
||
private static void appConfig(File configFile) { | ||
try (FileInputStream is = new FileInputStream(configFile)) { | ||
LogManager logManager = LogManager.getLogManager(); | ||
logManager.reset(); // close & remove existing handlers, reset existing logger levels | ||
logManager.readConfiguration(is); | ||
} catch (SecurityException | IOException e) { | ||
warning("Caught exception when reading logging properties: %s", configFile | ||
.getAbsolutePath()); | ||
e.printStackTrace(System.err); | ||
} | ||
} | ||
|
||
/** | ||
* Manually Configure the System Level requirements for java.util.logging. | ||
*/ | ||
private static void systemConfig() throws IOException { | ||
// Since System Loggers can arrive from deep within the various compat layers, it | ||
// makes sense to use the ServiceLoader to obtain references to specific System loggers | ||
// that need to be initialized during this step in the logging initialization. | ||
|
||
int count = 0; | ||
ServiceLoader<SystemLogger> serviceLoader = ServiceLoader.load(SystemLogger.class); | ||
for (SystemLogger systemLogger : serviceLoader) { | ||
debug("Executing SystemLogger: %s", systemLogger.getClass().getName()); | ||
systemLogger.configure(); | ||
count++; | ||
} | ||
debug("Initialized %d SystemLogger(s)", count); | ||
} | ||
|
||
/** | ||
* Non JUL debug logging. | ||
* <p> | ||
* As this class sets up the java.util.logging, | ||
* we can't reliable start using logging yet. | ||
* </p> | ||
* <p> | ||
* Note: by default this does not produce any output. | ||
* Set the System.property {@code com.google.apphosting.logging.DEBUG} to | ||
* true to see this output. | ||
* </p> | ||
* | ||
* @param format the format of the debug line | ||
* @param args the arguments of the debug line | ||
*/ | ||
private static void debug(String format, Object... args) { | ||
if (DEBUG) { | ||
System.err.printf("[CoreLogging:DEBUG] " + format + "%n", args); | ||
} | ||
} | ||
|
||
/** | ||
* Non JUL warnings logging. | ||
* <p> | ||
* As this class sets up the java.util.logging, | ||
* we can't reliable start using logging yet. | ||
* </p> | ||
* | ||
* @param format the format of the debug line | ||
* @param args the arguments of the debug line | ||
*/ | ||
private static void warning(String format, Object... args) { | ||
System.err.printf("[CoreLogging:WARNING] " + format + "%n", args); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
appengine-java-logging/src/main/java/com/google/apphosting/logging/SystemLogger.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* 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.logging; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* Interface for any component wanting to participate in the System Logging level | ||
* configuration. | ||
*/ | ||
public interface SystemLogger { | ||
/** | ||
* Configure your System Level Logger on this event. | ||
* | ||
* @throws IOException if unable to configure the System Level Logger | ||
*/ | ||
void configure() throws IOException; | ||
} |
37 changes: 37 additions & 0 deletions
37
...ngine-java-logging/src/test/java/com/google/apphosting/logging/CommonsLoggingExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* 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.logging; | ||
|
||
import org.apache.commons.logging.Log; | ||
import org.apache.commons.logging.LogFactory; | ||
|
||
import java.util.concurrent.ThreadLocalRandom; | ||
|
||
class CommonsLoggingExample implements Runnable { | ||
private static final Log logger = LogFactory.getLog(CommonsLoggingExample.class); | ||
|
||
@Override | ||
public void run() { | ||
ThreadLocalRandom rand = ThreadLocalRandom.current(); | ||
logger.trace(String.format("A CommonsLogging Trace Event: %d", rand.nextInt())); | ||
logger.debug(String.format("A CommonsLogging Debug Event: %d", rand.nextInt())); | ||
logger.info(String.format("A CommonsLogging Info Event: %d", rand.nextInt())); | ||
logger.warn(String.format("A CommonsLogging Warn Event: %d", rand.nextInt())); | ||
logger.error(String.format("A CommonsLogging Error Event: %d", rand.nextInt()), | ||
new RuntimeException("Generic Error")); | ||
} | ||
} |
Oops, something went wrong.