Tracks
/
F#
F#
/
Exercises
/
Log Levels
Log Levels

Log Levels

Learning Exercise

Introduction

Strings

A string in F# is an object that represents immutable text as a sequence of Unicode characters (letters, digits, punctuation, etc.) and is defined as follows:

let fruit = "Apple"

Strings are manipulated by either calling the string's methods, or using the String module's functions. Once a string has been constructed, its value can never change. Any methods/functions that appear to modify a string will actually return a new string.

Strings can be concatenated or formatted a few different ways.

  • You can use the + operator for concatenation:
let sentence = "Apple" + " is a type of fruit."
  • You can use the sprintf function for formatting text, where you have placeholders in a format text (%s for a string) and provide the values as arguments:
let sentence = sprintf "%s is a type of %s." "Apple" "fruit"

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 message function to return a log line's message:

message "[ERROR]: Invalid operation"
// => "Invalid operation"

Any leading or trailing white space should be removed:

message "[WARNING]:  Disk almost full\r\n"
// => "Disk almost full"

2. Get log level from a log line

Implement the logLevel function to return a log line's log level, which should be returned in lowercase:

logLevel "[ERROR]: Invalid operation"
// => "error"

3. Reformat a log line

Implement the reformat function that reformats the log line, putting the message first and the log level after it in parentheses:

reformat "[INFO]: Operation completed"
// => "Operation completed (info)"
Edit via GitHub The link opens in a new window or tab
F# Exercism

Ready to start Log Levels?

Sign up to Exercism to learn and master F# with 15 concepts, 142 exercises, and real human mentoring, all for free.