All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.dennisit.vplus.data.log4j.LogTruck Maven / Gradle / Ivy

/*--------------------------------------------------------------------------
 *  Copyright (c) 2010-2020, Elon.su All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the elon developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: Elon.su, you can also mail [email protected]
 *--------------------------------------------------------------------------
*/
package com.github.dennisit.vplus.data.log4j;

import com.github.dennisit.vplus.data.utils.StringUtils;
import com.google.common.collect.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public final class LogTruck implements Serializable{

    private static final Logger LOG = LoggerFactory.getLogger(LogTruck.class);
    private static Integer TAGS_SIZE = 1024;
    private static String LOG_TRUCK = "#LOG#";
    private final Map tagsMap = new HashMap();

    private LogTruck(){

    }

    public static LogTruck builder() {
        return new LogTruck();
    }

    public LogTruck seed(String seed){
        if(StringUtils.isBlank(seed)){
            seed = LOG_TRUCK;
        }
        LOG_TRUCK = seed;
        return this;
    }

    public LogTruck tag(String k, Object v) {
        v = null == v ? (String)null : String.valueOf(v);
        return tag(k, String.valueOf(v));
    }

    public LogTruck tag(String k, boolean v){
        return tag(k, v ? "true" : "false");
    }

    public LogTruck tag(String k, char c) {
        return tag(k, String.valueOf(c));
    }

    public LogTruck tag(String k, int v) {
        return tag(k, String.valueOf(v));
    }

    public LogTruck tag(String k, long v) {
        return tag(k, String.valueOf(v));
    }

    public LogTruck tag(String k, float v) {
        return tag(k, String.valueOf(v));
    }

    public LogTruck tag(String k, double v) {
        return tag(k, String.valueOf(v));
    }

    public LogTruck tag(String k, char v[]) {
        return tag(k, new String(v));
    }

    public LogTruck tag(String k, byte v[]) {
        return tag(k, new String(v, StandardCharsets.UTF_8));
    }

    public LogTruck tag(String k, String v) {
        k = colation(k);
        v = colation(v);
        if (StringUtils.isBlank(k)) {
            return this;
        }
        if (isTagsFull()) {
            LOG.error("no space left in LogTruck tags(max=1024) map for {}={}", k, v);
            return this;
        }
        tagsMap.put(k, v);
        return this;
    }

    public LogTruck tag(Map tags) {
        if (null == tags || tags.isEmpty()) {
            return this;
        }
        for (Map.Entry entry : tags.entrySet()) {
            this.tag(entry.getKey(), entry.getValue());
        }
        return this;
    }

    public LogTruck action(String action){
        return tag("Action", action);
    }

    public Map getTags() {
        return tagsMap;
    }

    public String getTag(String k) {
        return tagsMap.get(colation(k));
    }

    private String colation( String s) {
        if (StringUtils.isEmpty(s)) {
            return StringUtils.EMPTY;
        }
        return s.replaceAll("=",StringUtils.EMPTY).replaceAll(StringUtils.SPACE, StringUtils.EMPTY) ;
    }

    private boolean isTagsFull(){
        if(getTags().isEmpty()){
            return false;
        }
        return getTags().size() > TAGS_SIZE;
    }

    public String toString() {
        List list = Lists.newArrayList();
        getTags().forEach((k,v)->{
            list.add(k + "=" + v);
        });
        return LOG_TRUCK + " = {" + StringUtils.join(list, StringUtils.SPACE) + "}";

    }

    public String build(){
        return toString();
    }


    public static void main(String[] args) {
        LogTruck logTruck = new LogTruck().action("测,   试 :").tag("索引2", 2);
        System.out.println(logTruck);
        System.out.println(LogTruck.builder().tag("A索引1", 1).tag("索引2", 2).action("发一个"));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy