org.ocsoft.olivia.logger.OliviaLog.java Source code

Java tutorial

Introduction

Here is the source code for org.ocsoft.olivia.logger.OliviaLog.java

Source

/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.ocsoft.olivia.logger;

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;

import javax.annotation.Nonnull;
import javax.annotation.concurrent.Immutable;

import org.ocsoft.olivia.utils.JsonUtils;

import com.fasterxml.jackson.core.JsonProcessingException;

/**
 * Olivia??????.
 * @author tohhy
 */
@Immutable
public class OliviaLog {

    /**
     * ???.
     */
    @Nonnull
    private final LogLevel level;

    /**
     * ????.
     */
    @Nonnull
    private final Map<String, String> logData = new LinkedHashMap<String, String>();

    /**
     * ?ID??????.
     * @param level 
     * @param id ID
     * @param message 
     * @param logData 
     */
    public OliviaLog(LogLevel level, String id, String message, Map<String, String> logData) {
        if (level == null)
            throw new IllegalArgumentException("level must not be null");
        if (id == null || id.length() == 0)
            throw new IllegalArgumentException("id must not be empty");
        if (message == null || message.length() == 0)
            throw new IllegalArgumentException("message must not be empty");

        this.level = level;
        this.logData.put("id", id);
        this.logData.put("message", message);

        if (logData != null) {
            if (logData.containsKey("id"))
                throw new IllegalArgumentException("duplicate id data");
            if (logData.containsKey("message"))
                throw new IllegalArgumentException("duplicate message data");
            this.logData.putAll(logData);
        }
    }

    /**
     * ?ID?????.<br>
     * ?????.
     * @param level 
     * @param id ID
     * @param message 
     */
    public OliviaLog(LogLevel level, String id, String message) {
        this(level, id, message, null);
    }

    /**
     * ?????.
     * @return ???
     */
    @Nonnull
    public LogLevel getLevel() {
        return level;
    }

    /**
     * ???ID??.
     * @return ???ID
     */
    @Nonnull
    public String getID() {
        String message = logData.get("id");
        assert (message != null);
        return message;
    }

    /**
     * ?????.
     * @return ???
     */
    @Nonnull
    public String getMessage() {
        String message = logData.get("message");
        assert (message != null);
        return message;
    }

    /**
     * ??????.<br>
     * ?????????.
     * @param locale ?
     * @return ????
     */
    @Nonnull
    public String getMessage(Locale locale) {
        String message = logData.get("message." + locale.getLanguage());
        if (message == null)
            return getMessage();
        return message;
    }

    /**
     * ??????????.<br>
     * data?"isStatus":"true"????true.
     * @return ????????
     */
    public boolean isStatusMessage() {
        String s = logData.get("isStatus");
        return (s != null && s.toLowerCase().equals("true")) ? true : false;
    }

    /**
     * ???????????.
     * @return ??????
     */
    @Nonnull
    public Map<String, String> getData() {
        return Collections.unmodifiableMap(logData);
    }

    /**
     * ??????JSON???.
     * @return JSON???????
     */
    @Nonnull
    public String getDataAsJsonString() {
        try {
            return JsonUtils.toJsonString(logData);
        } catch (JsonProcessingException e) {
            // ??Map?????Jackson??????????????
            OliviaLogger.throwException(new AssertionError("something wrong in jackson parser", e));
            return "{}";
        }
    }

}