-
Notifications
You must be signed in to change notification settings - Fork 464
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |