-
Notifications
You must be signed in to change notification settings - Fork 546
/
LogResource.java
129 lines (114 loc) · 4.23 KB
/
LogResource.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
* Sonatype Nexus (TM) Open Source Version
* Copyright (c) 2008-present Sonatype, Inc.
* All rights reserved. Includes the third-party code listed at http://links.sonatype.com/products/nexus/oss/attributions.
*
* This program and the accompanying materials are made available under the terms of the Eclipse Public License Version 1.0,
* which accompanies this distribution and is available at http://www.eclipse.org/legal/epl-v10.html.
*
* Sonatype Nexus (TM) Professional Version is available from Sonatype, Inc. "Sonatype" and "Sonatype Nexus" are trademarks
* of Sonatype, Inc. Apache Maven is a trademark of the Apache Software Foundation. M2eclipse is a trademark of the
* Eclipse Foundation. All other trademarks are the property of their respective owners.
*/
package org.sonatype.nexus.logging.rest;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;
import org.sonatype.nexus.log.LogManager;
import org.sonatype.nexus.log.LoggerLevel;
import org.sonatype.nexus.logging.LoggingPlugin;
import org.sonatype.nexus.logging.model.MarkerXO;
import org.sonatype.sisu.goodies.common.ComponentSupport;
import org.sonatype.sisu.siesta.common.Resource;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.codehaus.plexus.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
import static javax.ws.rs.core.MediaType.APPLICATION_XML;
import static javax.ws.rs.core.MediaType.TEXT_PLAIN;
/**
* Log REST resource.
*
* @since 2.7
*/
@Named
@Singleton
@Path(LogResource.RESOURCE_URI)
public class LogResource
extends ComponentSupport
implements Resource
{
public static final String RESOURCE_URI = LoggingPlugin.REST_PREFIX + "/log";
private static final Logger log = LoggerFactory.getLogger(LogResource.class);
private final LogManager logManager;
@Inject
public LogResource(final LogManager logManager) {
this.logManager = checkNotNull(logManager);
}
/**
* Downloads a part of nexus.log (specified by fromByte/bytesCount) or full nexus.log (if fromByte/bytesCount are
* null).
*
* @param fromByte starting position
* @param bytesCount number of bytes
* @return part or full nexus.log
* @throws Exception If getting log fails
*/
@GET
@Produces({TEXT_PLAIN})
@RequiresPermissions(LoggingPlugin.PERMISSION_PREFIX_LOG + "read")
public Response get(final @QueryParam("fromByte") Long fromByte,
final @QueryParam("bytesCount") Long bytesCount)
throws Exception
{
Long from = fromByte;
if (from == null || from < 0) {
from = 0L;
}
Long count = bytesCount;
if (count == null) {
count = Long.MAX_VALUE;
}
return Response.ok(logManager.getApplicationLogAsStream("nexus.log", from, count).getInputStream())
.header("Content-Disposition", "attachment; filename=\"nexus.log\"")
.build();
}
/**
* Logs a message at INFO level.
*
* @param marker message to be logger (cannot be null/empty)
* @throws NullPointerException If marker is null
* @throws IllegalArgumentException If marker message is null or empty
*/
@PUT
@Path("/mark")
@Consumes({APPLICATION_JSON, APPLICATION_XML})
@Produces({APPLICATION_XML, APPLICATION_JSON})
@RequiresPermissions(LoggingPlugin.PERMISSION_PREFIX_LOGGERS + "update")
public void put(final MarkerXO marker)
throws Exception
{
checkNotNull(marker);
checkArgument(StringUtils.isNotEmpty(marker.getMessage()));
// ensure that level for marking logger is enabled
logManager.setLoggerLevel(log.getName(), LoggerLevel.INFO);
final String msg = StringEscapeUtils.escapeHtml(marker.getMessage());
String asterixes = StringUtils.repeat("*", msg.length() + 4);
log.info("\n"
+ asterixes + "\n"
+ "* " + msg + " *" + "\n"
+ asterixes
);
}
}