001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017 package org.apache.logging.log4j.core.pattern;
018
019 import java.util.UUID;
020
021 import org.apache.logging.log4j.core.LogEvent;
022 import org.apache.logging.log4j.core.config.plugins.Plugin;
023 import org.apache.logging.log4j.core.util.UuidUtil;
024
025 /**
026 * Formats the event sequence number.
027 */
028 @Plugin(name = "UuidPatternConverter", category = PatternConverter.CATEGORY)
029 @ConverterKeys({ "u", "uuid" })
030 public final class UuidPatternConverter extends LogEventPatternConverter {
031
032 private final boolean isRandom;
033
034 /**
035 * Private constructor.
036 */
037 private UuidPatternConverter(final boolean isRandom) {
038 super("u", "uuid");
039 this.isRandom = isRandom;
040 }
041
042 /**
043 * Obtains an instance of SequencePatternConverter.
044 *
045 * @param options options, currently ignored, may be null.
046 * @return instance of SequencePatternConverter.
047 */
048 public static UuidPatternConverter newInstance(final String[] options) {
049 if (options.length == 0) {
050 return new UuidPatternConverter(false);
051 }
052
053 if (options.length > 1 || (!options[0].equalsIgnoreCase("RANDOM") && !options[0].equalsIgnoreCase("Time"))) {
054 LOGGER.error("UUID Pattern Converter only accepts a single option with the value \"RANDOM\" or \"TIME\"");
055 }
056 return new UuidPatternConverter(options[0].equalsIgnoreCase("RANDOM"));
057 }
058
059 /**
060 * {@inheritDoc}
061 */
062 @Override
063 public void format(final LogEvent event, final StringBuilder toAppendTo) {
064 final UUID uuid = isRandom ? UUID.randomUUID() : UuidUtil.getTimeBasedUuid();
065 toAppendTo.append(uuid.toString());
066 }
067 }