Project

General

Profile

1
package eu.dnetlib.msro.logging;
2

    
3
import java.util.Date;
4
import java.util.HashMap;
5
import java.util.Map;
6

    
7
import org.apache.commons.lang3.StringUtils;
8

    
9
public class LogMessage {
10

    
11
	private final DnetLoggerDao dao;
12
	private final String logname;
13
	private final Map<String, Object> details;
14

    
15
	public static final String LOG_LEVEL_FIELD = "log:level";
16
	public static final String LOG_DATE_FIELD = "log:date";
17

    
18
	public LogMessage(final DnetLoggerDao dao, final String logname, final DnetLogLevel level) {
19
		this.dao = dao;
20
		this.details = new HashMap<>();
21
		this.details.put(LOG_LEVEL_FIELD, level.toString());
22
		this.details.put(LOG_DATE_FIELD, (new Date()).getTime());
23
		this.logname = logname;
24
	}
25

    
26
	public LogMessage addDetail(final String name, final String value) {
27
		if (StringUtils.isNotBlank(name) && value != null) {
28
			this.details.put(name, value);
29
		}
30
		return this;
31
	}
32

    
33
	public LogMessage addDetails(final Map<String, String> map) {
34
		if (map != null) {
35
			map.entrySet().forEach(e -> addDetail(e.getKey(), e.getValue()));
36
		}
37
		return this;
38
	}
39

    
40
	public void flush() {
41
		this.dao.writeLog(this.logname, this.details);
42
	}
43

    
44
}
(7-7/7)