Skip to content

Commit

Permalink
pumb to v5.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
alessandro-bottamedi committed Oct 17, 2024
1 parent 1782194 commit 8e5e824
Show file tree
Hide file tree
Showing 10 changed files with 3,578 additions and 2,170 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ dist
.vscode

.DS_store
.idea
.idea

.yarn
.pnp.*
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
## [5.2.0] - 17-10-2024

- Ensures JSON.stringify print nested objects correctly (issue #97)
- Only merge non undefined config values (pr #105 by @SYoder1)
- Correct README for Sentry logging (pr #104 by @ssorallen)
- Add crashlytics transport (pr #91 by @chad-aijinet)
- Added fileNameDateType option to the file transport for selecting the date format
- Minor bugfix

## [5.1.0] - 26-01-2024

- Ensures JSON.stringify correctly (Thanks @iago-f-s-e)
Expand Down
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -371,11 +371,12 @@ If you want a new file to be created every day you can use `{date-today}` in the

#### Accepted Options:

| name | type | description | default |
| -------- | ------ | -------------------------------------------------------------------------- | ------------------------------------------------------------------- |
| FS | Object | MANDATORY, filesystem instance for the transport (RNFS or expo FileSystem) | `null` |
| fileName | string | set logs file name (insert `{date-today}` for current date) | `log` |
| filePath | string | set logs file path | `RNFS.DocumentDirectoryPath` or expo `FileSystem.documentDirectory` |
| name | type | description | default |
| ---------------- | --------------- | --------------------------------------------------------------------------------- | ------------------------------------------------------------------- |
| FS | Object | MANDATORY, filesystem instance for the transport (RNFS or expo FileSystem) | `null` |
| fileName | string | set logs file name (insert `{date-today}` for current date) |
| fileNameDateType | `eu`,`us`,`iso` | `{date-today}` date type `eu` "DD-MM-YYYY", `us` "MM-DD-YYYY", `iso` "YYYY-MM-DD" | `eu` |
| filePath | string | set logs file path | `RNFS.DocumentDirectoryPath` or expo `FileSystem.documentDirectory` |

#### Example:

Expand Down
5 changes: 5 additions & 0 deletions demo/demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,9 @@ userLog.error("User wrong password");

rootLog.info("Log Object:", { a: 1, b: 2 });

rootLog.info("Log nested Object:", {
a: 1,
b: [{ name: "test", id: 1, arr: [{ arrId: 1 }] }],
});

rootLog.info("Multiple", "strings", ["array1", "array2"]);
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-logs",
"version": "5.1.0",
"version": "5.2.0",
"description": "Performance-aware simple logger for React-Native with namespaces, custom levels and custom transports (colored console, file writing, etc.)",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down Expand Up @@ -38,6 +38,6 @@
"homepage": "https://github.com/mowispace/react-native-logs#readme",
"devDependencies": {
"jest": "^29.7.0",
"typescript": "^5.3.3"
"typescript": "^5.6.3"
}
}
24 changes: 21 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,22 @@ let asyncFunc = (cb: Function) => {
}, 0);
};

function stringifyAllProperties(obj: any): string {
return JSON.stringify(
obj,
function (key, value) {
if (value && typeof value === "object") {
return Object.getOwnPropertyNames(value).reduce((acc: any, prop) => {
acc[prop] = value[prop];
return acc;
}, {});
}
return value;
},
2
);
}

let stringifyFunc = (msg: any): string => {
let stringMsg = "";
if (typeof msg === "string") {
Expand All @@ -50,8 +66,7 @@ let stringifyFunc = (msg: any): string => {
stringMsg = msg.message + " ";
} else {
try {
stringMsg =
"\n" + JSON.stringify(msg, Object.getOwnPropertyNames(msg), 2) + "\n";
stringMsg = "\n" + stringifyAllProperties(msg) + "\n";
} catch (error) {
stringMsg += "Undefined Message";
}
Expand Down Expand Up @@ -550,7 +565,10 @@ const createLogger = <Y extends string>(config?: configLoggerType) => {
};

// Merge all non undefined values
const mergedConfig = { ...defaultLogger, ...JSON.parse(JSON.stringify(config)) };
const mergedConfig = {
...defaultLogger,
...JSON.parse(JSON.stringify(config)),
};

return new logs(mergedConfig) as unknown as Omit<logs, "extend"> &
loggerType &
Expand Down
2 changes: 1 addition & 1 deletion src/transports/crashlyticsTransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const crashlyticsTransport: transportFunctionType = (props) => {
return true;
} catch (error) {
throw Error(
`react-native-logs: crashlyticsTransport - Error oon send msg to crashlytics`
`react-native-logs: crashlyticsTransport - Error on send msg to crashlytics`
);
}
};
Expand Down
23 changes: 18 additions & 5 deletions src/transports/fileAsyncTransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,23 @@ const RNFSappend = async (FS: any, file: string, msg: string) => {
}
};

const dateReplacer = (filename: string, type: "eu" | "us" | "iso") => {
let today = new Date();
let d = today.getDate();
let m = today.getMonth() + 1;
let y = today.getFullYear();
switch (type) {
case "eu":
return filename.replace("{date-today}", `${d}-${m}-${y}`);
case "us":
return filename.replace("{date-today}", `${m}-${d}-${y}`);
case "iso":
return filename.replace("{date-today}", `${y}-${m}-${d}`);
default:
return filename.replace("{date-today}", `${d}-${m}-${y}`);
}
};

const fileAsyncTransport: transportFunctionType = (props) => {
if (!props) return false;

Expand Down Expand Up @@ -85,12 +102,8 @@ const fileAsyncTransport: transportFunctionType = (props) => {
}

if (props?.options?.fileName) {
let today = new Date();
let d = today.getDate();
let m = today.getMonth() + 1;
let y = today.getFullYear();
fileName = props.options.fileName;
fileName = fileName.replace("{date-today}", `${d}-${m}-${y}`);
fileName = dateReplacer(fileName, props.options?.fileNameDateType);
}

if (props?.options?.filePath) filePath = props.options.filePath;
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"compilerOptions": {
"target": "es2015",
"target": "es2017",
"module": "commonjs",
"declaration": true,
"outDir": "./dist",
Expand Down
Loading

0 comments on commit 8e5e824

Please sign in to comment.