Skip to content

Commit 7941db5

Browse files
authored
Merge pull request #4 from cca-io/rescript
re-logger -> rescript-logger
2 parents cf47302 + e41e49f commit 7941db5

File tree

8 files changed

+181
-180
lines changed

8 files changed

+181
-180
lines changed

README.md

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,29 @@
1-
# re-logger
1+
# rescript-logger
22

3-
Simple logger library for [Reason](https://reasonml.github.io)/[BuckleScript](https://bucklescript.github.io) apps.
3+
Simple logger library for [ReScript](https://rescript-lang.org/) apps.
4+
5+
## Design Goals
6+
7+
1. Do not require a PPX, but still provide a way to include the module name in the log output.
8+
2. Pluggable logger implementation so that code shared between e.g. React and React Native can use a platform-specific implementation.
49

510
## Installation
611

712
```shell
813
# yarn
9-
yarn add @cca-io/re-logger
14+
yarn add @cca-io/rescript-logger
1015

1116
# or npm
12-
npm install @cca-io/re-logger
17+
npm install @cca-io/rescript-logger
1318
```
1419

15-
Then add re-logger to the dependencies in your `bsconfig.json`, e.g.:
20+
Then add rescript-logger to the dependencies in your `bsconfig.json`, e.g.:
1621

1722
```
1823
{
1924
"name": "my-app",
2025
...
21-
"bs-dependencies": ["reason-react", "@cca-io/re-logger"],
26+
"bs-dependencies": ["@rescript/react", "@cca-io/rescript-logger"],
2227
...
2328
}
2429
```
@@ -27,18 +32,18 @@ Then add re-logger to the dependencies in your `bsconfig.json`, e.g.:
2732

2833
Example:
2934

30-
```reason
31-
module Log = (val ReLogger.make(__MODULE__));
35+
```rescript
36+
module Log = unpack(ResLogger.make(__MODULE__))
3237
33-
Log.info("Starting");
38+
Log.info("Starting")
3439
35-
/* ... */
40+
// ...
3641
37-
Log.error2("Startup error", error);
42+
Log.error2("Startup error", error)
3843
```
3944

4045
The actual logger implementation is pluggable, so it can be swapped out e.g. for usage with React Native:
4146

42-
```reason
43-
ReLogger.setLoggerImpl((module MyNativeLoggerImpl));
47+
```rescript
48+
ResLogger.setLoggerImpl(module(MyNativeLoggerImpl))
4449
```

bsconfig.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
2-
"name": "@cca-io/re-logger",
2+
"name": "@cca-io/rescript-logger",
33
"sources": ["src"],
44
"package-specs": {
5-
"module": "commonjs",
5+
"module": "es6",
66
"in-source": true
77
},
88
"suffix": ".bs.js"

package.json

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,32 @@
11
{
2-
"name": "@cca-io/re-logger",
3-
"version": "1.2.0",
4-
"description": "Simple logger library for Reason/BuckleScript apps",
5-
"main": "src/ReLogger.bs.js",
6-
"repository": "https://github.com/cca-io/re-logger.git",
2+
"name": "@cca-io/rescript-logger",
3+
"version": "2.0.0",
4+
"description": "Simple logger library for ReScript apps",
5+
"main": "src/ResLogger.res",
6+
"repository": "https://github.com/cca-io/rescript-logger.git",
77
"author": "Christoph Knittel <[email protected]>",
88
"license": "MIT",
99
"private": false,
1010
"scripts": {
11-
"bs:build": "bsb",
12-
"bs:clean": "bsb -clean",
13-
"format": "bsrefmt --in-place src/*.re"
11+
"build": "rescript build",
12+
"watch": "rescript build -w",
13+
"clean": "rescript clean",
14+
"format": "rescript format -all"
1415
},
1516
"peerDependencies": {
16-
"bs-platform": ">=5.0.4"
17+
"rescript": ">=9.1.2"
1718
},
1819
"devDependencies": {
19-
"bs-platform": "^8.0.0"
20+
"rescript": "^9.1.2"
2021
},
2122
"files": [
2223
"src",
2324
"bsconfig.json"
2425
],
2526
"keywords": [
26-
"reason",
27-
"reasonml",
27+
"rescript",
2828
"log",
2929
"logger",
30-
"logging",
31-
"ocaml",
32-
"bucklescript"
30+
"logging"
3331
]
3432
}

src/ReLogger.re

Lines changed: 0 additions & 119 deletions
This file was deleted.

src/ReLogger.rei

Lines changed: 0 additions & 27 deletions
This file was deleted.

src/ResLogger.res

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
type level =
2+
| Debug
3+
| Info
4+
| Warn
5+
| Error
6+
7+
module type LoggerImpl = {
8+
let log: (. level, string) => unit
9+
let log2: (. level, string, 'a) => unit
10+
}
11+
12+
module DefaultImpl: LoggerImpl = {
13+
@val @scope("process.env") external nodeEnv: string = "NODE_ENV"
14+
@send external padStart: (string, int, string) => string = "padStart"
15+
16+
let prependDate = message => {
17+
let now = Js.Date.make()
18+
19+
let hours = now->Js.Date.getHours->Js.Float.toString->padStart(2, "0")
20+
let minutes = now->Js.Date.getMinutes->Js.Float.toString->padStart(2, "0")
21+
let seconds = now->Js.Date.getSeconds->Js.Float.toString->padStart(2, "0")
22+
23+
`${hours}:${minutes}:${seconds} ${message}`
24+
}
25+
26+
let log = (. level: level, msg: string) => {
27+
let msg = msg->prependDate
28+
29+
switch level {
30+
| Debug =>
31+
if nodeEnv === "development" {
32+
Js.log(msg)
33+
}
34+
| Info => Js.Console.info(msg)
35+
| Warn => Js.Console.warn(msg)
36+
| Error => Js.Console.error(msg)
37+
}
38+
}
39+
40+
let log2 = (. level: level, msg: string, obj: 'a) => {
41+
let msg = msg->prependDate
42+
43+
switch level {
44+
| Debug =>
45+
if nodeEnv === "development" {
46+
Js.log2(msg, obj)
47+
}
48+
| Info => Js.Console.info2(msg, obj)
49+
| Warn => Js.Console.warn2(msg, obj)
50+
| Error => Js.Console.error2(msg, obj)
51+
}
52+
}
53+
}
54+
55+
let loggerImpl: ref<module(LoggerImpl)> = ref(module(DefaultImpl: LoggerImpl))
56+
57+
let setLoggerImpl = (impl: module(LoggerImpl)) => loggerImpl := impl
58+
59+
module type Logger = {
60+
let debug: string => unit
61+
let info: string => unit
62+
let warn: string => unit
63+
let error: string => unit
64+
65+
let debug2: (string, 'a) => unit
66+
let info2: (string, 'a) => unit
67+
let warn2: (string, 'a) => unit
68+
let error2: (string, 'a) => unit
69+
}
70+
71+
let make = (moduleName: string): module(Logger) =>
72+
module(
73+
{
74+
let prefix = `[${moduleName}] `
75+
let prependModuleName = message => prefix ++ message
76+
77+
let debug = message => {
78+
let module(I) = loggerImpl.contents
79+
I.log(. Debug, message->prependModuleName)
80+
}
81+
82+
let info = message => {
83+
let module(I) = loggerImpl.contents
84+
I.log(. Info, message->prependModuleName)
85+
}
86+
87+
let warn = message => {
88+
let module(I) = loggerImpl.contents
89+
I.log(. Warn, message->prependModuleName)
90+
}
91+
92+
let error = message => {
93+
let module(I) = loggerImpl.contents
94+
I.log(. Error, message->prependModuleName)
95+
}
96+
97+
let debug2 = (message, obj) => {
98+
let module(I) = loggerImpl.contents
99+
I.log2(. Debug, message->prependModuleName, obj)
100+
}
101+
102+
let info2 = (message, obj) => {
103+
let module(I) = loggerImpl.contents
104+
I.log2(. Info, message->prependModuleName, obj)
105+
}
106+
107+
let warn2 = (message, obj) => {
108+
let module(I) = loggerImpl.contents
109+
I.log2(. Warn, message->prependModuleName, obj)
110+
}
111+
112+
let error2 = (message, obj) => {
113+
let module(I) = loggerImpl.contents
114+
I.log2(. Error, message->prependModuleName, obj)
115+
}
116+
}
117+
)

0 commit comments

Comments
 (0)