Skip to content

Commit

Permalink
build(core): support scala native
Browse files Browse the repository at this point in the history
  • Loading branch information
ThijsBroersen committed Nov 23, 2024
1 parent 2b72b7b commit db31114
Show file tree
Hide file tree
Showing 48 changed files with 350 additions and 246 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ jobs:
with:
fetch-depth: '0'
- name: Compile additional subprojects
run: sbt ++${{ matrix.scala }} examplesCore/compile examplesJpl/compile examplesJulBridge/compile examplesSlf4j2Bridge/compile examplesSlf4jLogback/compile examplesSlf4j2Logback/compile examplesSlf4j2Log4j/compile benchmarks/compile
run: sbt ++${{ matrix.scala }} examplesCoreJVM/compile examplesCoreJS/compile examplesCoreNative/compile examplesJpl/compile examplesJulBridge/compile examplesSlf4j2Bridge/compile examplesSlf4jLogback/compile examplesSlf4j2Logback/compile examplesSlf4j2Log4j/compile benchmarks/compile
update-readme:
name: Update README
runs-on: ubuntu-22.04
Expand Down
39 changes: 31 additions & 8 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,17 @@ lazy val root = project
.aggregate(
coreJVM,
coreJS,
coreNative,
slf4j,
slf4j2,
slf4jBridge,
slf4j2Bridge,
jpl,
julBridge,
benchmarks,
examplesCore,
examplesCoreJVM,
examplesCoreJS,
examplesCoreNative,
examplesJpl,
examplesJulBridge,
examplesSlf4j2Bridge,
Expand All @@ -67,7 +70,7 @@ lazy val root = project
docs
)

lazy val core = crossProject(JSPlatform, JVMPlatform)
lazy val core = crossProject(JSPlatform, JVMPlatform, NativePlatform)
.crossType(CrossType.Full)
.in(file("core"))
.settings(stdSettings(Some("zio-logging"), turnCompilerWarningIntoErrors = false))
Expand All @@ -84,8 +87,14 @@ lazy val core = crossProject(JSPlatform, JVMPlatform)
mimaSettings(failOnProblem = true)
)

lazy val coreJVM = core.jvm
lazy val coreJS = core.js.settings(
lazy val coreJVM = core.jvm
lazy val coreJS = core.js.settings(
libraryDependencies ++= Seq(
"io.github.cquiroz" %%% "scala-java-time" % scalaJavaTimeVersion % Test,
("org.scala-js" %%% "scalajs-java-securerandom" % "1.0.0" % Test).cross(CrossVersion.for3Use2_13)
)
)
lazy val coreNative = core.native.settings(
libraryDependencies += "io.github.cquiroz" %%% "scala-java-time" % scalaJavaTimeVersion % Test
)

Expand Down Expand Up @@ -180,19 +189,33 @@ lazy val benchmarks = project
.dependsOn(coreJVM)
.enablePlugins(JmhPlugin)

lazy val examplesCore = project
lazy val examplesCore = crossProject(JSPlatform, JVMPlatform, NativePlatform)
.crossType(CrossType.Full)
.in(file("examples/core"))
.dependsOn(coreJVM)
.dependsOn(core % "compile->compile;test->test")
.settings(stdSettings(Some("zio-logging-examples-core"), turnCompilerWarningIntoErrors = false))
.settings(enableZIO())
.settings(
publish / skip := true,
publish / skip := true
)

lazy val examplesCoreJVM = examplesCore.jvm
.settings(
libraryDependencies ++= Seq(
"dev.zio" %% "zio-metrics-connectors-prometheus" % zioMetricsConnectorsVersion,
"dev.zio" %% "zio-http" % zioHttp,
"dev.zio" %% "zio-config-typesafe" % zioConfig
)
)
lazy val examplesCoreJS = examplesCore.js
.settings(
scalaJSUseMainModuleInitializer := true,
libraryDependencies += "io.github.cquiroz" %%% "scala-java-time" % scalaJavaTimeVersion
)
lazy val examplesCoreNative = examplesCore.native
.settings(
libraryDependencies += "io.github.cquiroz" %%% "scala-java-time" % scalaJavaTimeVersion
)

lazy val examplesSlf4jLogback = project
.in(file("examples/slf4j-logback"))
Expand Down Expand Up @@ -280,7 +303,7 @@ lazy val compileExamplesJob = Def.setting {
SingleStep(
name = "Compile additional subprojects",
run = Some(
"sbt ++${{ matrix.scala }} examplesCore/compile examplesJpl/compile examplesJulBridge/compile " +
"sbt ++${{ matrix.scala }} examplesCoreJVM/compile examplesCoreJS/compile examplesCoreNative/compile examplesJpl/compile examplesJulBridge/compile " +
"examplesSlf4j2Bridge/compile examplesSlf4jLogback/compile examplesSlf4j2Logback/compile " +
"examplesSlf4j2Log4j/compile benchmarks/compile"
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright 2019-2024 John A. De Goes and the ZIO Contributors
*
* 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 zio.logging

private[logging] trait LoggingPackagePlatformSpecific
170 changes: 170 additions & 0 deletions core/jvm-native/src/main/scala/zio/logging/FileLoggerLayers.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
/*
* Copyright 2019-2024 John A. De Goes and the ZIO Contributors
*
* 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 zio.logging

import zio.{ Config, NonEmptyChunk, Queue, Runtime, Scope, UIO, ZIO, ZLayer, ZLogger }

import java.nio.charset.Charset
import java.nio.file.Path

private[logging] trait FileLoggerLayers {

def fileAsyncJsonLogger(config: FileLoggerConfig): ZLayer[Any, Nothing, Unit] =
makeFileAsyncJsonLogger(config).installUnscoped

def fileAsyncJsonLogger(configPath: NonEmptyChunk[String] = loggerConfigPath): ZLayer[Any, Config.Error, Unit] =
FileLoggerConfig.load(configPath).flatMap(makeFileAsyncJsonLogger).installUnscoped

def fileAsyncLogger(config: FileLoggerConfig): ZLayer[Any, Nothing, Unit] =
makeFileAsyncLogger(config).installUnscoped

def fileAsyncLogger(configPath: NonEmptyChunk[String] = loggerConfigPath): ZLayer[Any, Config.Error, Unit] =
FileLoggerConfig.load(configPath).flatMap(makeFileAsyncLogger).installUnscoped

def fileJsonLogger(config: FileLoggerConfig): ZLayer[Any, Nothing, Unit] =
makeFileJsonLogger(config).install

def fileJsonLogger(configPath: NonEmptyChunk[String] = loggerConfigPath): ZLayer[Any, Config.Error, Unit] =
FileLoggerConfig.load(configPath).flatMap(makeFileJsonLogger).install

def fileLogger(config: FileLoggerConfig): ZLayer[Any, Nothing, Unit] =
makeFileLogger(config).install

def fileLogger(configPath: NonEmptyChunk[String] = loggerConfigPath): ZLayer[Any, Config.Error, Unit] =
FileLoggerConfig.load(configPath).flatMap(makeFileLogger).install

def makeFileAsyncJsonLogger(config: FileLoggerConfig): ZIO[Scope, Nothing, FilteredLogger[String, Any]] =
makeFileAsyncLogger(
config.destination,
config.format.toJsonLogger,
config.charset,
config.autoFlushBatchSize,
config.bufferedIOSize,
config.rollingPolicy
).filter(config.toFilter)

def makeFileAsyncLogger(config: FileLoggerConfig): ZIO[Scope, Nothing, FilteredLogger[String, Any]] =
makeFileAsyncLogger(
config.destination,
config.format.toLogger,
config.charset,
config.autoFlushBatchSize,
config.bufferedIOSize,
config.rollingPolicy
).filter(config.toFilter)

def makeFileAsyncLogger(
destination: Path,
logger: ZLogger[String, String],
charset: Charset,
autoFlushBatchSize: Int,
bufferedIOSize: Option[Int],
rollingPolicy: Option[FileLoggerConfig.FileRollingPolicy]
): ZIO[Scope, Nothing, ZLogger[String, Any]] =
for {
queue <- Queue.bounded[UIO[Any]](1000)
_ <- queue.take.flatMap(task => task.ignore).forever.forkScoped
} yield fileWriterAsyncLogger(
destination,
logger,
charset,
autoFlushBatchSize,
bufferedIOSize,
queue,
rollingPolicy
)

private def fileWriterAsyncLogger(
destination: Path,
logger: ZLogger[String, String],
charset: Charset,
autoFlushBatchSize: Int,
bufferedIOSize: Option[Int],
queue: Queue[UIO[Any]],
rollingPolicy: Option[FileLoggerConfig.FileRollingPolicy]
): ZLogger[String, Any] = {
val logWriter =
new zio.logging.internal.FileWriter(destination, charset, autoFlushBatchSize, bufferedIOSize, rollingPolicy)

val stringLogger: ZLogger[String, Any] = logger.map { (line: String) =>
zio.Unsafe.unsafe { implicit u =>
Runtime.default.unsafe.run(queue.offer(ZIO.succeed {
try logWriter.writeln(line)
catch {
case t: VirtualMachineError => throw t
case _: Throwable => ()
}
}))
}
}
stringLogger
}

def makeFileJsonLogger(config: FileLoggerConfig): ZIO[Any, Nothing, FilteredLogger[String, Any]] =
makeFileLogger(
config.destination,
config.format.toJsonLogger,
config.charset,
config.autoFlushBatchSize,
config.bufferedIOSize,
config.rollingPolicy
).filter(config.toFilter)

def makeFileLogger(config: FileLoggerConfig): ZIO[Any, Nothing, FilteredLogger[String, Any]] =
makeFileLogger(
config.destination,
config.format.toLogger,
config.charset,
config.autoFlushBatchSize,
config.bufferedIOSize,
config.rollingPolicy
).filter(config.toFilter)

def makeFileLogger(
destination: Path,
logger: ZLogger[String, String],
charset: Charset,
autoFlushBatchSize: Int,
bufferedIOSize: Option[Int],
rollingPolicy: Option[FileLoggerConfig.FileRollingPolicy]
): ZIO[Any, Nothing, ZLogger[String, Any]] =
ZIO.succeed(
fileWriterLogger(destination, logger, charset, autoFlushBatchSize, bufferedIOSize, rollingPolicy)
)

private def fileWriterLogger(
destination: Path,
logger: ZLogger[String, String],
charset: Charset,
autoFlushBatchSize: Int,
bufferedIOSize: Option[Int],
rollingPolicy: Option[FileLoggerConfig.FileRollingPolicy]
): ZLogger[String, Any] = {
val logWriter =
new zio.logging.internal.FileWriter(destination, charset, autoFlushBatchSize, bufferedIOSize, rollingPolicy)

val stringLogger: ZLogger[String, Any] = logger.map { (line: String) =>
try logWriter.writeln(line)
catch {
case t: VirtualMachineError => throw t
case _: Throwable => ()
}
}

stringLogger
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright 2019-2024 John A. De Goes and the ZIO Contributors
*
* 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 zio.logging

private[logging] trait LoggingPackagePlatformSpecific extends FileLoggerLayers
8 changes: 4 additions & 4 deletions core/shared/src/main/scala/zio/logging/LogFormat.scala
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ sealed trait LogFormat { self =>
* the log output is highlighted.
*/
final def highlight: LogFormat =
highlight(defaultHighlighter)
highlight(LogFormat.defaultHighlighter)

/**
* The alphanumeric version of the `|-|` operator.
Expand Down Expand Up @@ -164,6 +164,9 @@ sealed trait LogFormat { self =>
)
builder.toString()
}
}

object LogFormat {

private val defaultHighlighter: LogLevel => LogColor = {
case LogLevel.Error => LogColor.RED
Expand All @@ -172,9 +175,6 @@ sealed trait LogFormat { self =>
case LogLevel.Debug => LogColor.GREEN
case _ => LogColor.WHITE
}
}

object LogFormat {

private[logging] def makeLogger(
fn: (
Expand Down
Loading

0 comments on commit db31114

Please sign in to comment.