Vue 3 UnicornLog by @WebDevNerdStuff

Vue 3 UnicornLog

1.0.0

A magical 🦄 plugin to make coloring the console output easier and more flexible.

Sometimes when building your application/site you don't want to see your linter complaining about using console functions. This helps to keep your linter happy so you can concentrate on writing bug free magical code.

It also has the ability to run the console functions depending on environment variables so you can keep your production site's console quiet.

This plugin is also available for Vue 2 here.

Demo
Open DevTools and click on the "Console" tab and try out some Examples.

Installation

Using pnpm:
Copy Code
				pnpm add vue3-unicorn-log
			
Using npm:
Copy Code
				npm i vue3-unicorn-log
			

Register Plugin

Code Block
Copy Code
				import { createApp } from 'vue';
import UnicornLog from 'vue3-unicorn-log';

createApp()
  .use(UnicornLog)
  .mount('#app');
			

Usage

Composition API (setup)
Copy Code
				<script setup>
import { inject } from 'vue';

const $unicornLog = inject('$unicornLog');

$unicornLog();
</script>
			
Composition API (script setup)
Copy Code
				<script>
import { inject } from 'vue';

export default {
	setup() {
		const $unicornLog = inject('$unicornLog');

		$unicornLog();

		return {};
	},
};
</script>
			
Options API
Copy Code
				<script>
export default {
	inject: ['$unicornLog'],
	mounted() {
		this.$unicornLog();
	},
};
</script>
			

Plugin Options

NameTypeDefaultOptionsDescription
defaultStylesObject
const rainbowLinearGradient = `linear-gradient(to right,
	hsl(0, 100%, 50%),
	hsl(60, 100%, 50%),
	hsl(120, 100%, 50%),
	hsl(180, 100%, 50%),
	hsl(240, 100%, 50%),
	hsl(300, 100%, 50%),
	hsl(360, 100%, 50%)
)`;

defaultStyles: {
	log: [
		'background-color: black',
		`border-image: ${rainbowLinearGradient} 1`,
		'border-style: solid',
		'border-width: 4px',
		'color: #fff',
		'font-weight: normal',
		'padding: 8px',
	],
	info: [
		'background-color: hsla(225, 100%, 8%, 1)',
		'box-shadow: 999px 0 0 hsla(225, 100%, 8%, 1)',
		'color: hsla(225, 100%, 85%, 1)',
		'display: block',
		'padding: 2px',
	],
}
{
	log: [],
	info: [],
}
Used to adjust the default styles.
disabledBooleanfalsetrue
false
Disables the output of the log in the console. This works best when using an environment to conditionally set so it will log in development, but not on the production site.
logPrefixBoolean | Stringfalse-Prepends a string to to the output.
stylesString | Array-Styling Console OutputSets the styles for the log.
typeStringlog clear
debug
dir
error
group
groupCollapsed
groupEnd
infos
log
table
trace
warn
Specifies which console method should be used.
Overriding the plugin default options
Copy Code
				import { createApp } from 'vue';
import UnicornLog from 'vue3-unicorn-log';

const app = createApp(App);

app.use(UnicornLog, {
	logPrefix: '[OMG LOOK HERE!]:',
	disabled: process​.env.UNICORN_LOG !== 'true',
	// ...other options
});

app.mount('#app');
			

Log Options

NameTypeDefaultOptionsDescription
arrayArray[]-Used to include an array in the log.
disabledBooleanfalsetrue
false
Disables the output of the log in the console. This works best when using an environment to conditionally set so it will log in development, but not on the production site.
logPrefixBoolean | Stringfalse-Prepends a string to to the output.
magicalBooleanfalsetrue
false
Adds a magical style to the output.
nameString[UnicornLog]:- If logPrefix option is set as a Boolean of true, it will use the name option for the prefix.
objectsObject{}-Used to include objects in the log.
stylesString | Array-Styling Console OutputSets the styles for the log.
textString🦄-Used to include a string in the log.
typeStringlog clear
debug
dir
error
group
groupCollapsed
groupEnd
infos
log
table
trace
warn
Specifies which console method should be used.

Console Methods

For a description of the different log methods, refer to the Web APIs MDN developer documentation for console.

Examples

Open DevTools and click on the "Console" tab to view example results.

Simple Example
Copy Code
Run
				$unicornLog();
			
text
Copy Code
Run
				$unicornLog({
	text: 'Hello World',
});
			
type
Copy Code
Run
				$unicornLog({
	text: 'Hello World',
	type: 'log',
});
			
styles
For a list of the different available styles, refer to the Web APIs MDN Styling Console Output developer documentation. Depending on the browser, some styles may not work.
Copy Code
Run
				$unicornLog({
	text: 'Hello World',
	styles: 'color: DeepPink; font-size: 2rem;',
});
			
Using an Array
Copy Code
Run
				$unicornLog({
	text: 'Hello World',
	styles: [
		'background: black',
		'border: 1px dashed magenta',
		'color: magenta',
		'font-family: monospace',
		'font-size: 2em',
		'padding: 10px',
	],
});
			
You can also set the style option to a magical word to make the magic happen. This can also be set with the magical option to true.
Copy Code
Run
				$unicornLog({
	text: 'Hello World',
	styles: 'unicorn',
});
			
disabled
Copy Code
Run
				$unicornLog({
	text: 'Hello World',
	disabled: true,
});
			
Using an .env variable
Conditionally set so it will log in development, but not on the production site.
				$unicornLog({
	text: 'Hello World',
	disabled: process​.env.UNICORN_LOG !== 'true',
});
			
logPrefix
Copy Code
Run
				$unicornLog({
	text: 'Hello World',
	logPrefix: '[Bunnies]:',
});
			
If logPrefix option is set as a Boolean of true, it will use the default name option for the prefix.
Copy Code
Run
				$unicornLog({
	text: 'Hello World',
	logPrefix: true,
});
			
Copy Code
Run
				$unicornLog({
	text: 'Hello World',
	logPrefix: true,
	name: 'Bob',
});
			
magical
Copy Code
Run
				$unicornLog({
	text: 'Hello World',
	magical: true,
});
			
name
Copy Code
Run
				$unicornLog({
	text: 'Hello World',
	logPrefix: true,
	name: 'Bob',
});
			
objects
Single object
Copy Code
Run
				$unicornLog({
	text: 'Hello World',
	objects: { foo: 'bar' },
});
			
Multiple objects
Copy Code
Run
				const foo = { foo: 'foo ' };
const bar = { bar: 'bar ' };

$unicornLog({
	text: 'Hello World',
	objects: { foo, bar },
});
			
Using object spread Operator
Copy Code
Run
				const foo = { foo: 'foo ' };
const bar = { bar: 'bar ' };

$unicornLog({
	text: 'Hello World',
	objects: { ...foo, ...bar },
});
			
array
Single array
Copy Code
Run
				$unicornLog({
	text: 'Hello World',
	array: ['foo', 'bar'],
});
			
Using an array of objects
Copy Code
Run
				const foo = { foo: 'foo ' };
const bar = { bar: 'bar ' };

$unicornLog({
	text: 'Hello World',
	array: [foo, bar],
});
			
Using array spread Operator
Copy Code
Run
				const foo = ['foo'];
const bar = ['bar'];

$unicornLog({
	text: 'Hello World',
	array: [...foo, ...bar],
});
			

Dependencies

Change Log

License

Copyright © 2023 WebDevNerdStuff
Licensed under the MIT license.

LICENSE.md

Vue 3 UnicornLog by @WebDevNerdStuff