Exercism.org-Java-Log Levels

Instructions

In this exercise you’ll be processing log-lines.

Each log line is a string formatted as follows: "[<LEVEL>]: <MESSAGE>".

There are three different log levels:

  • INFO
  • WARNING
  • ERROR

You have three tasks, each of which will take a log line and ask you to do something with it.

1. Get message from a log line

Implement the (staticLogLevels.message() method to return a log line’s message:

1
2
LogLevels.message("[ERROR]: Invalid operation")
// => "Invalid operation"

Any leading or trailing white space should be removed:

1
2
LogLevels.message("[WARNING]:  Disk almost full\r\n")
// => "Disk almost full"

2. Get log level from a log line

Implement the (staticLogLevels.logLevel() method to return a log line’s log level, which should be returned in lowercase:

1
2
LogLevels.logLevel("[ERROR]: Invalid operation")
// => "error"

3. Reformat a log line

Implement the (staticLogLevels.reformat() method that reformats the log line, putting the message first and the log level after it in parentheses:

1
2
LogLevels.reformat("[INFO]: Operation completed")
// => "Operation completed (info)"

Methods

Modifier and Type Method and Description
int indexOf(int ch)
Returns the index within this string of the first occurrence of the specified character.
int indexOf(int ch, int fromIndex)
Returns the index within this string of the first occurrence of the specified character, starting the search at the specified index.
int indexOf(String str)
Returns the index within this string of the first occurrence of the specified substring.
int indexOf(String str, int fromIndex)
Returns the index within this string of the first occurrence of the specified substring, starting at the specified index.
String substring(int beginIndex)
Returns a new string that a substring of this string.
String substring(int beginIndex,int endIndex)
Returns a new string that a substring of this string.
String trim()
Returns a copy of the string, with leading and trailing whitespace omitted.
String toLowerCase()
Converts all of the characters in this String to lower case using the rules of the default locale.
String toLowerCase()
Converts all of the characters in this String to lower case using the rules of the given Locale.

Question Answers

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
public class LogLevels {

// Method for task 1
public static String message(String logLine) {
// Extract message part by using substring method
String message = logLine.substring(logLine.indexOf(':') + 1).trim();

return message;
}

// Method for task 2
public static String logLevel(String logLine) {
// Extract level part by using substring method, then convert it to lowercase
String level = logLine.substring(logLine.indexOf('[') + 1, logLine.indexOf(']')).toLowerCase();

return level;
}

// Method for task 3
public static String reformat(String logLine) {
// Use above two methods to extract message and level, then reformat the log line
String message = message(logLine);
String level = logLevel(logLine);

return message + " (" + level + ")";
}
}

Word

process
n.
(为达到某一目标的)过程;进程;流程;工序;工艺流程;(事物发展,尤指自然变化的)步骤;做事方法
v.
处理(文件、请求等);加工;数据处理;审核;审阅
v.
列队行进;缓缓前进
adj.
(用化学方法等)处理过的;照相制版的;经过特殊加工的;三色版的

processing
v.
处理;审阅,审核,处理(文件、请求等);加工;数据处理
v.
列队行进;缓缓前进
n.
加工;处理;运算
process的现在分词

formatted
v.
格式化;安排…的版式
adj.
格式化(的);格式化了的;有格式的
format的过去分词和过去式

Implement
vt.
实施;执行;贯彻;使生效
n.
工具;器具;(常指)简单的户外用具

lowercase
n.
小写字母;小写字体

occurrence
n.
发生;出现;存在;发生的事情;存在的事物

  • 版权声明: 本博客所有文章除特别声明外,著作权归作者所有。转载请注明出处!

You Found Me.

支付宝
微信