com.sun.enterprise.server.logging.UniformLogFormatter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of payara-micro Show documentation
Show all versions of payara-micro Show documentation
Micro Distribution of the Payara Project for IBM JDK
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2006-2013 Oracle and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can
* obtain a copy of the License at
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
* or packager/legal/LICENSE.txt. See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at packager/legal/LICENSE.txt.
*
* GPL Classpath Exception:
* Oracle designates this particular file as subject to the "Classpath"
* exception as provided by Oracle in the GPL Version 2 section of the License
* file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*
* Contributor(s):
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
// Portions Copyright [2016] [Payara Foundation]
package com.sun.enterprise.server.logging;
import com.sun.common.util.logging.GFLogRecord;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.logging.ErrorManager;
import java.util.logging.Formatter;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.LogRecord;
import org.glassfish.api.VersionInfo;
import org.glassfish.hk2.api.PerLookup;
import org.glassfish.hk2.api.ServiceLocator;
import org.glassfish.internal.api.Globals;
import org.jvnet.hk2.annotations.ContractsProvided;
import org.jvnet.hk2.annotations.Service;
/**
* UniformLogFormatter conforms to the logging format defined by the
* Log Working Group in Java Webservices Org.
* The specified format is
* "[#|DATETIME|LOG_LEVEL|PRODUCT_ID|LOGGER NAME|OPTIONAL KEY VALUE PAIRS|
* MESSAGE|#]\n"
*
* @author Hemanth Puttaswamy
*
* TODO:
* 1. Performance improvement. We can Cache the LOG_LEVEL|PRODUCT_ID strings
* and minimize the concatenations and revisit for more performance
* improvements
* 2. Need to use Product Name and Version based on the version string
* that is part of the product.
* 3. Stress testing
* 4. If there is a Map as the last element, need to scan the message to
* distinguish key values with the message argument.
*/
@Service()
@ContractsProvided({UniformLogFormatter.class, Formatter.class})
@PerLookup
public class UniformLogFormatter extends Formatter implements LogEventBroadcaster {
private static final String RECORD_NUMBER = "RecordNumber";
private static final String METHOD_NAME = "MethodName";
private static final String CLASS_NAME = "ClassName";
private ServiceLocator habitat = Globals.getDefaultBaseServiceLocator();
// loggerResourceBundleTable caches references to all the ResourceBundle
// and can be searched using the LoggerName as the key
private HashMap loggerResourceBundleTable;
private LogManager logManager;
// A Dummy Container Date Object is used to format the date
private Date date = new Date();
private static boolean LOG_SOURCE_IN_KEY_VALUE = false;
private static boolean RECORD_NUMBER_IN_KEY_VALUE = false;
private FormatterDelegate _delegate = null;
static {
String logSource = System.getProperty(
"com.sun.aas.logging.keyvalue.logsource");
if ((logSource != null)
&& (logSource.equals("true"))) {
LOG_SOURCE_IN_KEY_VALUE = true;
}
String recordCount = System.getProperty(
"com.sun.aas.logging.keyvalue.recordnumber");
if ((recordCount != null)
&& (recordCount.equals("true"))) {
RECORD_NUMBER_IN_KEY_VALUE = true;
}
}
private long recordNumber = 0;
private static final String LINE_SEPARATOR = System.getProperty("line.separator");
private String recordBeginMarker;
private String recordEndMarker;
private String recordFieldSeparator;
private String recordDateFormat;
private static final String RECORD_BEGIN_MARKER = "[#|";
private static final String RECORD_END_MARKER = "|#]" + LINE_SEPARATOR;
private static final char FIELD_SEPARATOR = '|';
public static final char NVPAIR_SEPARATOR = ';';
public static final char NV_SEPARATOR = '=';
private static final String RFC_3339_DATE_FORMAT =
"yyyy-MM-dd'T'HH:mm:ss.SSSZ";
private LogEventBroadcaster logEventBroadcasterDelegate;
private boolean multiLineMode;
private static final String INDENT = " ";
private ExcludeFieldsSupport excludeFieldsSupport = new ExcludeFieldsSupport();
private String productId = "";
public UniformLogFormatter() {
super();
loggerResourceBundleTable = new HashMap();
logManager = LogManager.getLogManager();
}
public UniformLogFormatter(FormatterDelegate delegate) {
this();
_delegate = delegate;
}
public void setDelegate(FormatterDelegate delegate) {
_delegate = delegate;
}
/**
* _REVISIT_: Replace the String Array with an HashMap and do some
* benchmark to determine whether StringCat is faster or Hashlookup for
* the template is faster.
*/
@Override
public String format(LogRecord record) {
return uniformLogFormat(record);
}
@Override
public String formatMessage(LogRecord record) {
return uniformLogFormat(record);
}
/**
* GlassFish can override to specify their product version
*/
protected String getProductId() {
if (habitat != null) {
VersionInfo versionInfo = habitat.getService(VersionInfo.class);
if (productId.isEmpty() && versionInfo != null) {
StringBuilder sb = new StringBuilder();
sb.append(versionInfo.getAbbreviatedProductName());
sb.append(' ');
sb.append(versionInfo.getVersionPrefix());
sb.append(versionInfo.getMajorVersion());
sb.append('.');
sb.append(versionInfo.getMinorVersion());
productId = sb.toString();
}
}
return productId;
}
/**
* Sun One Appserver SE/EE? can override to specify their product specific
* key value pairs.
*/
protected void getNameValuePairs(StringBuilder buf, LogRecord record) {
Object[] parameters = record.getParameters();
if ((parameters == null) || (parameters.length == 0)) {
return;
}
try {
for (Object obj : parameters) {
if (obj == null) {
continue;
}
if (obj instanceof Map) {
for (Map.Entry
© 2015 - 2024 Weber Informatics LLC | Privacy Policy