Skip to content

Commit 8643b76

Browse files
committed
Merge branch 'main' of github.com:Tincre/logger
2 parents 06359c6 + 05b43c6 commit 8643b76

File tree

6 files changed

+316
-89
lines changed

6 files changed

+316
-89
lines changed

.github/workflows/codeql.yml

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# For most projects, this workflow file will not need changing; you simply need
2+
# to commit it to your repository.
3+
#
4+
# You may wish to alter this file to override the set of languages analyzed,
5+
# or to provide custom queries or build logic.
6+
#
7+
# ******** NOTE ********
8+
# We have attempted to detect the languages in your repository. Please check
9+
# the `language` matrix defined below to confirm you have the correct set of
10+
# supported CodeQL languages.
11+
#
12+
name: "CodeQL Advanced"
13+
14+
on:
15+
push:
16+
branches: [ "main" ]
17+
pull_request:
18+
branches: [ "main" ]
19+
schedule:
20+
- cron: '30 6 * * 1'
21+
22+
jobs:
23+
analyze:
24+
name: Analyze (${{ matrix.language }})
25+
# Runner size impacts CodeQL analysis time. To learn more, please see:
26+
# - https://gh.io/recommended-hardware-resources-for-running-codeql
27+
# - https://gh.io/supported-runners-and-hardware-resources
28+
# - https://gh.io/using-larger-runners (GitHub.com only)
29+
# Consider using larger runners or machines with greater resources for possible analysis time improvements.
30+
runs-on: ${{ (matrix.language == 'swift' && 'macos-latest') || 'ubuntu-latest' }}
31+
permissions:
32+
# required for all workflows
33+
security-events: write
34+
35+
# required to fetch internal or private CodeQL packs
36+
packages: read
37+
38+
# only required for workflows in private repositories
39+
actions: read
40+
contents: read
41+
42+
strategy:
43+
fail-fast: false
44+
matrix:
45+
include:
46+
- language: javascript-typescript
47+
build-mode: none
48+
# CodeQL supports the following values keywords for 'language': 'c-cpp', 'csharp', 'go', 'java-kotlin', 'javascript-typescript', 'python', 'ruby', 'swift'
49+
# Use `c-cpp` to analyze code written in C, C++ or both
50+
# Use 'java-kotlin' to analyze code written in Java, Kotlin or both
51+
# Use 'javascript-typescript' to analyze code written in JavaScript, TypeScript or both
52+
# To learn more about changing the languages that are analyzed or customizing the build mode for your analysis,
53+
# see https://docs.github.com/en/code-security/code-scanning/creating-an-advanced-setup-for-code-scanning/customizing-your-advanced-setup-for-code-scanning.
54+
# If you are analyzing a compiled language, you can modify the 'build-mode' for that language to customize how
55+
# your codebase is analyzed, see https://docs.github.com/en/code-security/code-scanning/creating-an-advanced-setup-for-code-scanning/codeql-code-scanning-for-compiled-languages
56+
steps:
57+
- name: Checkout repository
58+
uses: actions/checkout@v4
59+
60+
# Initializes the CodeQL tools for scanning.
61+
- name: Initialize CodeQL
62+
uses: github/codeql-action/init@v3
63+
with:
64+
languages: ${{ matrix.language }}
65+
build-mode: ${{ matrix.build-mode }}
66+
# If you wish to specify custom queries, you can do so here or in a config file.
67+
# By default, queries listed here will override any specified in a config file.
68+
# Prefix the list here with "+" to use these queries and those in the config file.
69+
70+
# For more details on CodeQL's query packs, refer to: https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs
71+
# queries: security-extended,security-and-quality
72+
73+
# If the analyze step fails for one of the languages you are analyzing with
74+
# "We were unable to automatically build your code", modify the matrix above
75+
# to set the build mode to "manual" for that language. Then modify this step
76+
# to build your code.
77+
# ℹ️ Command-line programs to run using the OS shell.
78+
# 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun
79+
- if: matrix.build-mode == 'manual'
80+
shell: bash
81+
run: |
82+
echo 'If you are using a "manual" build mode for one or more of the' \
83+
'languages you are analyzing, replace this with the commands to build' \
84+
'your code, for example:'
85+
echo ' make bootstrap'
86+
echo ' make release'
87+
exit 1
88+
89+
- name: Perform CodeQL Analysis
90+
uses: github/codeql-action/analyze@v3
91+
with:
92+
category: "/language:${{matrix.language}}"

.github/workflows/release.yml

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,33 @@
1-
# Reusable workflow for releases; to eject, you can replace this file with
2-
# https://github.com/ryansonshine/ryansonshine/blob/main/.github/workflows/release.yml
31
name: Release
42
on:
5-
push:
6-
branches:
7-
- main
3+
workflow_call:
4+
secrets:
5+
NPM_TOKEN:
6+
required: true
7+
88
jobs:
99
release:
10-
permissions:
11-
contents: write
12-
issues: write
13-
pull-requests: write
14-
uses: ryansonshine/ryansonshine/.github/workflows/release.yml@main
15-
secrets:
16-
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
10+
name: Release
11+
runs-on: ubuntu-latest
12+
steps:
13+
- name: Checkout
14+
uses: actions/checkout@v3
15+
with:
16+
fetch-depth: 0
17+
- name: Setup Node.js
18+
uses: actions/setup-node@v3
19+
with:
20+
node-version: lts/*
21+
- name: Install dependencies
22+
run: npm ci
23+
- name: Build
24+
run: npm run build
25+
- name: Test
26+
run: npm test
27+
- name: Release
28+
env:
29+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
30+
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
31+
run: npx semantic-release
32+
- name: Upload Code Coverage
33+
uses: codecov/codecov-action@v3.1.0

README.md

Lines changed: 97 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
# @tincre/logger
22

3-
[![npm package][npm-img]][npm-url]
4-
[![Build Status][build-img]][build-url]
5-
[![Downloads][downloads-img]][downloads-url]
6-
[![Issues][issues-img]][issues-url]
7-
[![Code Coverage][codecov-img]][codecov-url]
8-
[![Commitizen Friendly][commitizen-img]][commitizen-url]
3+
[![npm package][npm-img]][npm-url]
4+
[![Build Status][build-img]][build-url]
5+
[![Downloads][downloads-img]][downloads-url]
6+
[![Issues][issues-img]][issues-url]
7+
[![Code Coverage][codecov-img]][codecov-url]
8+
[![Commitizen Friendly][commitizen-img]][commitizen-url]
99
[![Semantic Release][semantic-release-img]][semantic-release-url]
1010

1111
## Install
@@ -19,42 +19,107 @@ npm install @tincre/logger
1919
```ts
2020
import { logger } from '@tincre/logger';
2121

22-
logger.debug('hello');
23-
//=> 'hello' in logging output
22+
logger.debug('Debugging message');
23+
logger.log('Application started');
24+
logger.warn('This is a warning');
25+
logger.error('Unexpected error occurred', new Error('Something went wrong'));
2426
```
2527

2628
## API
2729

28-
### myPackage(input, options?)
30+
### `logger.log(message, data?)`
2931

30-
#### input
32+
Logs a standard message to the console if `NODE_ENV` is **not** set to `production`.
3133

32-
Type: `string`
34+
#### Parameters:
3335

34-
Lorem ipsum.
36+
- `message`: **string** - The main log message.
37+
- `data` (optional): **unknown** - Additional data (object, array, string, etc.).
3538

36-
#### options
39+
#### Example:
3740

38-
Type: `object`
41+
```ts
42+
logger.log('User logged in', { userId: 123 });
43+
```
44+
45+
---
46+
47+
### `logger.error(message, data?)`
48+
49+
Logs an error message to the console if `NODE_ENV` is **not** set to `production`.
50+
51+
#### Parameters:
52+
53+
- `message`: **string** - The error message.
54+
- `data` (optional): **unknown** - Additional error details.
55+
56+
#### Example:
57+
58+
```ts
59+
logger.error('Database connection failed', new Error('Connection timeout'));
60+
```
61+
62+
---
63+
64+
### `logger.warn(message, data?)`
65+
66+
Logs a warning message to the console if `NODE_ENV` is **not** set to `production`.
67+
68+
#### Parameters:
69+
70+
- `message`: **string** - The warning message.
71+
- `data` (optional): **unknown** - Additional details.
72+
73+
#### Example:
74+
75+
```ts
76+
logger.warn('Low disk space', { availableGB: 2 });
77+
```
78+
79+
---
80+
81+
### `logger.debug(message, data?)`
82+
83+
Logs a debug message to the console if `NODE_ENV` is **not** set to `production`.
84+
85+
#### Parameters:
86+
87+
- `message`: **string** - The debug message.
88+
- `data` (optional): **unknown** - Additional debug info.
89+
90+
#### Example:
91+
92+
```ts
93+
logger.debug('Fetching API data', { endpoint: '/users', method: 'GET' });
94+
```
95+
96+
---
97+
98+
## Environment Variables
99+
100+
| Variable | Description |
101+
| ---------- | -------------------------------------------------------------------- |
102+
| `NODE_ENV` | Set to `"production"` to disable logging in production environments. |
103+
104+
---
39105

40-
##### postfix
106+
## Contributing
41107

42-
Type: `string`
43-
Default: `rainbows`
108+
We welcome contributions! Please follow our commit guidelines and open issues if you encounter any problems.
44109

45-
Lorem ipsum.
110+
---
46111

47-
[build-img]:https://github.com/ryansonshine/typescript-npm-package-template/actions/workflows/release.yml/badge.svg
48-
[build-url]:https://github.com/ryansonshine/typescript-npm-package-template/actions/workflows/release.yml
49-
[downloads-img]:https://img.shields.io/npm/dt/typescript-npm-package-template
50-
[downloads-url]:https://www.npmtrends.com/typescript-npm-package-template
51-
[npm-img]:https://img.shields.io/npm/v/typescript-npm-package-template
52-
[npm-url]:https://www.npmjs.com/package/typescript-npm-package-template
53-
[issues-img]:https://img.shields.io/github/issues/ryansonshine/typescript-npm-package-template
54-
[issues-url]:https://github.com/ryansonshine/typescript-npm-package-template/issues
55-
[codecov-img]:https://codecov.io/gh/ryansonshine/typescript-npm-package-template/branch/main/graph/badge.svg
56-
[codecov-url]:https://codecov.io/gh/ryansonshine/typescript-npm-package-template
57-
[semantic-release-img]:https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg
58-
[semantic-release-url]:https://github.com/semantic-release/semantic-release
59-
[commitizen-img]:https://img.shields.io/badge/commitizen-friendly-brightgreen.svg
60-
[commitizen-url]:http://commitizen.github.io/cz-cli/
112+
[build-img]: https://github.com/Tincre/logger/actions/workflows/release.yml/badge.svg
113+
[build-url]: https://github.com/Tincre/logger/actions/workflows/release.yml
114+
[downloads-img]: https://img.shields.io/npm/dt/@tincre/logger
115+
[downloads-url]: https://www.npmtrends.com/@tincre/logger
116+
[npm-img]: https://img.shields.io/npm/v/@tincre/logger
117+
[npm-url]: https://www.npmjs.com/package/@tincre/logger
118+
[issues-img]: https://img.shields.io/github/issues/Tincre/logger
119+
[issues-url]: https://github.com/Tincre/logger/issues
120+
[codecov-img]: https://codecov.io/gh/Tincre/logger/branch/main/graph/badge.svg
121+
[codecov-url]: https://codecov.io/gh/Tincre/logger
122+
[semantic-release-img]: https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg
123+
[semantic-release-url]: https://github.com/semantic-release/semantic-release
124+
[commitizen-img]: https://img.shields.io/badge/commitizen-friendly-brightgreen.svg
125+
[commitizen-url]: http://commitizen.github.io/cz-cli/

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@tincre/logger",
3-
"version": "0.0.0-development",
3+
"version": "0.0.5",
44
"description": "A template for creating npm packages using TypeScript and VSCode",
55
"main": "./lib/index.js",
66
"files": [

src/index.ts

Lines changed: 29 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -28,67 +28,56 @@
2828
* Each log message is prefixed with a timestamp and a label indicating the log level.
2929
*
3030
* Functions:
31-
* - `logMessage(message: string): void`: Logs a standard message.
32-
* - `errorMessage(message: string): void`: Logs an error message.
33-
* - `warnMessage(message: string): void`: Logs a warning message.
34-
* - `debugMessage(message: string): void`: Logs a debug message.
31+
* - `logMessage(message: string, data?: unknown): void`: Logs a standard message.
32+
* - `errorMessage(message: string, data?: unknown): void`: Logs an error message.
33+
* - `warnMessage(message: string, data?: unknown): void`: Logs a warning message.
34+
* - `debugMessage(message: string, data?: unknown): void`: Logs a debug message.
3535
*
3636
* The logger object aggregates these functions for easy access.
3737
*/
38+
type LoggerFunction = (message: string, data?: unknown) => void;
3839

39-
export const logger = {
40+
export const logger: Record<
41+
'debug' | 'log' | 'error' | 'warn',
42+
LoggerFunction
43+
> = {
4044
debug: debugMessage,
4145
log: logMessage,
4246
error: errorMessage,
4347
warn: warnMessage,
4448
};
45-
/**
46-
* Logs a message to the console if the NODE_ENV is not set to "production".
47-
*
48-
* @param message - The message to be logged.
49-
* @returns {void}
50-
*/
51-
function logMessage(message: string): void {
49+
50+
function formatMessage(level: string, message: string, data?: unknown): string {
51+
const timestamp = new Date().toISOString();
52+
const formattedData =
53+
data !== undefined
54+
? typeof data === 'object'
55+
? ` ${JSON.stringify(data)}`
56+
: ` ${String(data)}`
57+
: '';
58+
return `[${level.toUpperCase()}] ${timestamp}: ${message}${formattedData}`;
59+
}
60+
61+
function logMessage(message: string, data?: unknown): void {
5262
if (process.env.NODE_ENV !== 'production') {
53-
console.log(`[LOG] ${new Date().toISOString()}: ${message}`);
63+
console.log(formatMessage('LOG', message, data));
5464
}
5565
}
5666

57-
/**
58-
* Logs an error message to the console if the NODE_ENV is not set to "production".
59-
*
60-
* @param message - The error message to be logged.
61-
* @returns {void}
62-
*
63-
* @example
64-
* errorMessage("Failed to connect to the database");
65-
*/
66-
function errorMessage(message: string): void {
67+
function errorMessage(message: string, data?: unknown): void {
6768
if (process.env.NODE_ENV !== 'production') {
68-
console.error(`[ERROR] ${new Date().toISOString()}: ${message}`);
69+
console.error(formatMessage('ERROR', message, data));
6970
}
7071
}
7172

72-
/**
73-
* Logs a warning message to the console if the NODE_ENV is not set to "production".
74-
*
75-
* @param message - The warning message to be logged.
76-
* @returns {void}
77-
*/
78-
function warnMessage(message: string): void {
73+
function warnMessage(message: string, data?: unknown): void {
7974
if (process.env.NODE_ENV !== 'production') {
80-
console.warn(`[WARN] ${new Date().toISOString()}: ${message}`);
75+
console.warn(formatMessage('WARN', message, data));
8176
}
8277
}
8378

84-
/**
85-
* Logs a debug message to the console if the NODE_ENV is not set to "production".
86-
*
87-
* @param message - The debug message to be logged.
88-
* @returns {void}
89-
*/
90-
function debugMessage(message: string): void {
79+
function debugMessage(message: string, data?: unknown): void {
9180
if (process.env.NODE_ENV !== 'production') {
92-
console.debug(`[DEBUG] ${new Date().toISOString()}: ${message}`);
81+
console.debug(formatMessage('DEBUG', message, data));
9382
}
9483
}

0 commit comments

Comments
 (0)