Skip to content

Commit

Permalink
Add MarkdownLogWriter
Browse files Browse the repository at this point in the history
  • Loading branch information
ar committed Jun 13, 2024
1 parent 8f86ea0 commit e776db2
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions jpos/src/main/java/org/jpos/util/MarkdownLogWriter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* jPOS Project [http://jpos.org]
* Copyright (C) 2000-2022 jPOS Software SRL
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package org.jpos.util;

import org.jpos.iso.ISODate;
import org.jpos.log.LogRenderer;
import org.jpos.log.LogRendererRegistry;

import java.io.PrintStream;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;

public class MarkdownLogWriter implements LogEventWriter {
private PrintStream ps;
private final LogRenderer<LogEvent> renderer = LogRendererRegistry.getRenderer(LogEvent.class, LogRenderer.Type.MARKDOWN);
private Instant start;

@Override
public void write(LogEvent ev) {
renderer.render(ev, ps, "");
}

@Override
public void setPrintStream(PrintStream ps) {
this.ps = ps;
start = Instant.now();
ps.printf ("# Log Start %s (%d)%n", LocalDateTime.ofInstant(start, ZoneId.systemDefault()), ps.hashCode());
}

@Override
public void close() {
Instant now = Instant.now();
ps.printf ("# Log End %s (%s)%n",
LocalDateTime.ofInstant(now, ZoneId.systemDefault()),
ISODate.formatDuration(Duration.between(start,now))
);
if (ps != System.out && ps != System.err) {
ps.close();
}
}
}

0 comments on commit e776db2

Please sign in to comment.