001/*
002 * jPOS Project [http://jpos.org]
003 * Copyright (C) 2000-2026 jPOS Software SRL
004 *
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU Affero General Public License as
007 * published by the Free Software Foundation, either version 3 of the
008 * License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 * GNU Affero General Public License for more details.
014 *
015 * You should have received a copy of the GNU Affero General Public License
016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017 */
018
019package org.jpos.log.render.json;
020
021import com.fasterxml.jackson.core.JsonProcessingException;
022import com.fasterxml.jackson.databind.ObjectMapper;
023import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
024import org.jpos.log.AuditLogEvent;
025import org.jpos.log.LogRenderer;
026
027import java.io.PrintStream;
028
029public final class AuditLogEventJsonLogRenderer implements LogRenderer<AuditLogEvent> {
030    private final ObjectMapper mapper = new ObjectMapper();
031
032    public AuditLogEventJsonLogRenderer () {
033        mapper.registerModule(new JavaTimeModule());
034        mapper.disable(com.fasterxml.jackson.databind.SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
035    }
036
037    @Override
038    public void render(AuditLogEvent evt, PrintStream ps, String indent) {
039        try {
040            ps.print (mapper.writeValueAsString(evt));
041        } catch (JsonProcessingException e) {
042            ps.print (kv("exception", e.toString()));
043        }
044    }
045    public Class<?> clazz() {
046        return AuditLogEvent.class;
047    }
048    public Type type() {
049        return Type.JSON;
050    }
051
052    private String kv (String k, String v) {
053        return "{\"%s\":\"%s\"}".formatted(k,v);
054    }
055}