Skip to content

Commit

Permalink
Merge pull request #584 from danicheg/merge-series/1.x-into-main
Browse files Browse the repository at this point in the history
Merge `series/1.x` into `main`
  • Loading branch information
danicheg authored Jan 13, 2022
2 parents 37493dc + cd7b86e commit 0bce3e2
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,29 @@ object MyThing {
}
```

### Laconic syntax

It's possible to use interpolated syntax for logging.
Currently, supported ops are: `trace`, `debug`, `info`, `warn`, `error`.
You can use it for your custom `Logger` as well as for Slf4j `Logger`.

```scala
import cats.Applicative
import cats.effect.Sync
import org.typelevel.log4cats.Logger
import org.typelevel.log4cats.syntax._

def successComputation[F[_]: Applicative]: F[Int] = Applicative[F].pure(1)
def errorComputation[F[_]: Sync]: F[Unit] = Sync[F].raiseError[Unit](new Throwable("Sorry!"))

def log[F[_]: Sync: Logger] =
for {
result1 <- successComputation[F]
_ <- info"First result is $result1"
_ <- errorComputation[F].onError(_ => error"We got an error!")
} yield ()
```

## CVE-2021-44228 ("log4shell")

log4cats is not directly susceptible to CVS-2021-44228. The
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2018 Typelevel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.typelevel.log4cats

package object syntax {
implicit final class LoggerInterpolator(private val sc: StringContext) extends AnyVal {
def error[F[_]](message: Any*)(implicit logger: Logger[F]): F[Unit] =
logger.error(sc.s(message: _*))

def warn[F[_]](message: Any*)(implicit logger: Logger[F]): F[Unit] =
logger.warn(sc.s(message: _*))

def info[F[_]](message: Any*)(implicit logger: Logger[F]): F[Unit] =
logger.info(sc.s(message: _*))

def debug[F[_]](message: Any*)(implicit logger: Logger[F]): F[Unit] =
logger.debug(sc.s(message: _*))

def trace[F[_]](message: Any*)(implicit logger: Logger[F]): F[Unit] =
logger.trace(sc.s(message: _*))
}
}
25 changes: 25 additions & 0 deletions docs/src/main/mdoc/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,31 @@ def passForEasierUse[F[_]: Sync: Logger] = for {
} yield something
```

### Laconic syntax

It's possible to use interpolated syntax for logging.
Currently, supported ops are: `trace`, `debug`, `info`, `warn`, `error`.
You can use it for your custom `Logger` as well as for Slf4j `Logger`.

```scala mdoc
import cats.Applicative
import cats.effect._
import org.typelevel.log4cats.Logger
import org.typelevel.log4cats.syntax._

def successComputation[F[_]: Applicative]: F[Int] = Applicative[F].pure(1)
def errorComputation[F[_]: Sync]: F[Unit] = Sync[F].raiseError[Unit](new Throwable("Sorry!"))

def log[F[_]: Sync: Logger] =
for {
result1 <- successComputation[F]
_ <- info"First result is $result1"
_ <- errorComputation[F].onError {
case _ => error"We got an error!"
}
} yield ()
```

## CVE-2021-44228 ("log4shell")

log4cats is not directly susceptible to CVS-2021-44228. The
Expand Down

0 comments on commit 0bce3e2

Please sign in to comment.