source: ComponentRegistry/trunk/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/frontend/ViewLogPage.java @ 2755

Last change on this file since 2755 was 2755, checked in by twagoo, 11 years ago

Improved reading and serving of log file in the wicket admin frontend
[Fixes #294]

File size: 2.8 KB
Line 
1package clarin.cmdi.componentregistry.frontend;
2
3import java.io.File;
4import java.io.FileNotFoundException;
5import java.io.IOException;
6import java.io.RandomAccessFile;
7import java.text.NumberFormat;
8import org.apache.log4j.FileAppender;
9import org.apache.log4j.Logger;
10import org.apache.wicket.PageParameters;
11import org.apache.wicket.markup.html.basic.Label;
12import org.apache.wicket.markup.html.form.TextArea;
13import org.apache.wicket.markup.html.link.DownloadLink;
14import org.apache.wicket.markup.html.link.Link;
15import org.apache.wicket.model.Model;
16
17/**
18 *
19 * @author Twan Goosen <twan.goosen@mpi.nl>
20 * @author paucas
21 */
22public class ViewLogPage extends SecureAdminWebPage {
23
24    public ViewLogPage(final PageParameters pageParameters) {
25        super(pageParameters);
26        addLinks();
27        addLogFileContent();
28    }
29
30    private void addLinks() {
31        add(new Link("home") {
32            @Override
33            public void onClick() {
34                setResponsePage(AdminHomePage.class);
35            }
36        });
37    }
38
39    private void addLogFileContent() {
40        final int tailSize = 1000000; // Megabyte
41
42        final NumberFormat numberFormat = NumberFormat.getIntegerInstance();
43        numberFormat.setGroupingUsed(true);
44
45        final File logFile = getLogFile();
46        if (logFile != null) {
47            try {
48                final RandomAccessFile raLogFile = geFileTail(logFile, tailSize);
49                final String content = getLogFileContent(raLogFile);
50
51                add(new Label("logLabel", String.format("Showing final %s bytes (or less) of total %s in %s:", numberFormat.format(tailSize), numberFormat.format(raLogFile.length()), logFile)));
52                add(new TextArea("logText", new Model(content)));
53
54                add(new DownloadLink("logDownloadLink", logFile));
55            } catch (IOException ioEx) {
56                add(new Label("logLabel", "Could not read from log file. See error message below."));
57                add(new TextArea("logText", new Model(ioEx.getMessage())));
58            }
59        }
60    }
61
62    private String getLogFileContent(final RandomAccessFile randomAccessFile) throws IOException {
63        String currentLine;
64        StringBuilder contentBuilder = new StringBuilder();
65        while ((currentLine = randomAccessFile.readLine()) != null) {
66            contentBuilder.append(currentLine).append("\n");
67        }
68        String content = contentBuilder.toString();
69        return content;
70    }
71
72    private RandomAccessFile geFileTail(File logFile, int tailLength) throws IOException, FileNotFoundException {
73        // Skip to tail of file
74        final RandomAccessFile raLogFile = new RandomAccessFile(logFile, "r");
75        final long startPosition = raLogFile.length() - tailLength;
76        if (startPosition > 0) {
77            raLogFile.seek(startPosition);
78            // Read until end of line so we don't end up halfway some random line
79            raLogFile.readLine();
80        }
81        return raLogFile;
82    }
83
84    private File getLogFile() {
85        // Get file from appender
86        final FileAppender appender = (FileAppender) Logger.getRootLogger().getAppender("ROOT");
87        if (appender != null) {
88            return new File(appender.getFile());
89        }
90        return null;
91    }
92}
Note: See TracBrowser for help on using the repository browser.